ikruglov / YAHC

YAHC - Yet another HTTP client
MIT License
12 stars 10 forks source link

HTTP data sent to HTTPS protocol #9

Closed merkys closed 5 years ago

merkys commented 5 years ago

I send the following request using HTTPS protocol, however, the server replies that it receives HTTP requests instead of HTTPS:

$request = {
          'method' => 'GET',
          'ssl_options' => {
                             'SSL_server' => 1,
                             'SSL_hostname' => $hostname,
                             'SSL_verifycn_scheme' => 'https',
                             'SSL_verifycn_name' => $hostname
                           },
          'port' => 443,
          'scheme' => 'https',
          'query_string' => undef,
          'callback' => $callback,
          'path' => $request_url,
          'head' => [
                      'Accept',
                      'application/vnd.api+json',
                      'X-PONAPI-Client-Version',
                      '1.0',
                      'X-PONAPI-Escaped-Values',
                      '1'
                    ],
          'host' => $hostname
        };

Server replies with:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
Reason: You're speaking plain HTTP to an SSL-enabled server port.<br />
 Instead use the HTTPS scheme to access this URL, please.<br />
</p>
<hr>
<address>Apache/2.4.10 (Debian) Server at solsa.crystallography.net Port 443</address>
</body></html>

This seems to be the symptom of an HTTP request to a HTTPS port. I am using YAHC 0.035.

ikruglov commented 5 years ago

try send the request while enabling debugging:

   debug => 1,
merkys commented 5 years ago

I have tried with the debug, and everything seems OK to me. Pasted the debug output below:

[2019-05-06 13:18:56.73149] [38764] YAHC connection 'connection_38764000': new state STATE_INIT
[2019-05-06 13:18:56.82164] [38764] YAHC connection 'connection_38764000': Target https://solsa.crystallography.net:443 (193.219.81.217:443) chosen for attempt #1
[2019-05-06 13:18:56.82171] [38764] YAHC connection 'connection_38764000': build new socket
[2019-05-06 13:18:56.82182] [38764] YAHC connection 'connection_38764000': new state STATE_CONNECTING
[2019-05-06 13:18:56.82189] [38764] YAHC: pid 38764 entering event loop
[2019-05-06 13:18:56.82295] [38764] YAHC connection 'connection_38764000': new state STATE_CONNECTED
[2019-05-06 13:18:56.82301] [38764] YAHC connection 'connection_38764000': new state STATE_SSL_HANDSHAKE
[2019-05-06 13:18:56.82305] [38764] YAHC connection 'connection_38764000': start SSL handshake with options: SSL_server=1, SSL_verifycn_name=solsa.crystallography.net, SSL_verifycn_scheme=https, SSL_hostname=solsa.crystallography.net
[2019-05-06 13:18:56.824  ] [38764] YAHC connection 'connection_38764000': SSL handshake successfully completed
[2019-05-06 13:18:56.82403] [38764] YAHC connection 'connection_38764000': new state STATE_WRITING
[2019-05-06 13:18:56.82407] [38764] YAHC connection 'connection_38764000': writing body of 201 bytes
GET https://solsa.crystallography.net/db-trunk/samples/sample/1 HTTP/1.1
Accept: application/vnd.api+json
X-PONAPI-Client-Version: 1.0
X-PONAPI-Escaped-Values: 1
Host: solsa.crystallography.net

[2019-05-06 13:18:56.82412] [38764] YAHC connection 'connection_38764000': new state STATE_READING
[2019-05-06 13:18:56.8283 ] [38764] YAHC connection 'connection_38764000': headers parsed: 400 HTTP/1.1 headers=server='Apache/2.4.10 (Debian)' date='Mon, 06 May 2019 10:18:56 GMT' content-length='454' content-type='text/html; charset=iso-8859-1' connection='close'
[2019-05-06 13:18:56.82834] [38764] YAHC connection 'connection_38764000': new state STATE_USER_ACTION
[2019-05-06 13:18:56.82836] [38764] YAHC connection 'connection_38764000': drop socket
[2019-05-06 13:18:56.82837] [38764] YAHC connection 'connection_38764000': call callback

Then follows the 400 Bad Request message as attached in the initial message, followed by:

[2019-05-06 13:18:56.8285 ] [38764] YAHC connection 'connection_38764000': after invoking callback state is STATE_USER_ACTION
[2019-05-06 13:18:56.82852] [38764] YAHC connection 'connection_38764000': new state STATE_COMPLETED
[2019-05-06 13:18:56.82862] [38764] YAHC: pid 38764 exited from event loop after 5 iterations

I am able to reproduce the 400 HTTP error with plain curl by requesting the http:// on 443 port.

ikruglov commented 5 years ago

very strange, I see no problem in HTTPS communications. YAHC established SSL session:

SSL handshake successfully completed

then it sent the request:

 writing body of 201 bytes

and received a response:

headers parsed: 400 HTTP/1.1 headers=server='Apache/2.4.10 (Debian)' date='Mon, 06 May 2019 10:18:56 GMT' content-length='454' content-type='text/html; charset=iso-8859-1' connection='close'

One thing which stood out for me is that you put URL in the path field. And this is how we got this body:

GET https://solsa.crystallography.net/db-trunk/samples/sample/1 HTTP/1.1

whereas it should have been:

GET /db-trunk/samples/sample/1 HTTP/1.1

try using path instead of URL.

merkys commented 5 years ago
GET /db-trunk/samples/sample/1 HTTP/1.1

This results in the same response. Actually, even non-existing paths produce the same output.

merkys commented 5 years ago

The problem must have been an incorrect directive 'SSL_server' => 1. Sorry for the noise.