hewei.it 4 anni fa
parent
commit
852f697bbb

+ 4 - 5
http/HttpMessage.cpp

@@ -315,14 +315,13 @@ std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
 }
 
 void HttpRequest::DumpUrl() {
-    if (url.size() != 0 && strncmp(url.c_str(), "http", 4) == 0) {
+    if (url.size() != 0 && strstr(url.c_str(), "://") != NULL) {
         // have been complete url
         return;
     }
     std::string str;
     // scheme://
-    str += "http";
-    if (https) str += 's';
+    str = scheme;
     str += "://";
     // host:port
     char c_str[256] = {0};
@@ -367,13 +366,13 @@ void HttpRequest::ParseUrl() {
     http_parser_url_init(&parser);
     http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
     // scheme
-    https = !strncmp(url.c_str(), "https", 5);
+    scheme = url.substr(parser.field_data[UF_SCHEMA].off, parser.field_data[UF_SCHEMA].len);
     // host
     if (parser.field_set & (1<<UF_HOST)) {
         host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
     }
     // port
-    port = parser.port ? parser.port : https ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
+    port = parser.port ? parser.port : strcmp(scheme.c_str(), "https") ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT;
     // path
     if (parser.field_set & (1<<UF_PATH)) {
         path = url.c_str() + parser.field_data[UF_PATH].off;

+ 2 - 2
http/HttpMessage.h

@@ -246,7 +246,7 @@ public:
     // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
     std::string         url;
     // structured url
-    bool                https;
+    std::string         scheme;
     std::string         host;
     int                 port;
     std::string         path;
@@ -264,7 +264,7 @@ public:
         headers["User-Agent"] = DEFAULT_USER_AGENT;
         headers["Accept"] = "*/*";
         method = HTTP_GET;
-        https = 0;
+        scheme = "http";
         host = "127.0.0.1";
         port = DEFAULT_HTTP_PORT;
         path = "/";

+ 1 - 1
http/client/AsyncHttpClient.cpp

@@ -47,7 +47,7 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
         hio_set_peeraddr(connio, &peeraddr.sa, sockaddr_len(&peeraddr));
         addChannel(connio);
         // https
-        if (req->https) {
+        if (strcmp(req->scheme.c_str(), "https") == 0) {
             hio_enable_ssl(connio);
         }
     }

+ 9 - 24
http/client/WebSocketClient.cpp

@@ -1,6 +1,5 @@
 #include "WebSocketClient.h"
 
-#include "http_parser.h" // for http_parser_url
 #include "base64.h"
 #include "hlog.h"
 
@@ -35,28 +34,18 @@ int WebSocketClient::open(const char* _url) {
         }
     }
     hlogi("%s", url.c_str());
-    http_parser_url parser;
-    http_parser_url_init(&parser);
-    http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
-    // scheme
-    bool wss = !strncmp(url.c_str(), "wss", 3);
-    // host
-    std::string host = "127.0.0.1";
-    if (parser.field_set & (1<<UF_HOST)) {
-        host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
-    }
-    // port
-    int port = parser.port ? parser.port : wss ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
-    // path
-    std::string path = "/";
-    if (parser.field_set & (1<<UF_PATH)) {
-        path = url.c_str() + parser.field_data[UF_PATH].off;
-    }
+    http_req_.reset(new HttpRequest);
+    // ws => http
+    http_req_->url = "http" + url.substr(2, -1);
+    http_req_->ParseUrl();
 
-    int connfd = createsocket(port, host.c_str());
+    int connfd = createsocket(http_req_->port, http_req_->host.c_str());
     if (connfd < 0) {
         return connfd;
     }
+
+    // wss
+    bool wss = strncmp(url.c_str(), "wss", 3) == 0;
     if (wss) {
         withTLS();
     }
@@ -65,10 +54,6 @@ int WebSocketClient::open(const char* _url) {
         if (channel->isConnected()) {
             state = CONNECTED;
             // websocket_handshake
-            http_req_.reset(new HttpRequest);
-            http_req_->method = HTTP_GET;
-            // ws => http
-            http_req_->url = "http" + url.substr(2, -1);
             http_req_->headers["Connection"] = "Upgrade";
             http_req_->headers["Upgrade"] = "websocket";
             // generate SEC_WEBSOCKET_KEY
@@ -105,7 +90,7 @@ int WebSocketClient::open(const char* _url) {
             }
             if (http_parser_->IsComplete()) {
                 if (http_resp_->status_code != HTTP_STATUS_SWITCHING_PROTOCOLS) {
-                    hloge("server side not support websockt!");
+                    hloge("server side not support websocket!");
                     channel->close();
                     return;
                 }

+ 6 - 5
http/client/http_client.cpp

@@ -119,9 +119,9 @@ int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
     if (!cli || !req || !resp) return ERR_NULL_POINTER;
 
     if (req->url.empty() || *req->url.c_str() == '/') {
+        req->scheme = cli->https ? "https" : "http";
         req->host = cli->host;
         req->port = cli->port;
-        req->https = cli->https;
     }
 
     if (req->timeout == 0) {
@@ -322,7 +322,7 @@ static int __http_client_connect(http_client_t* cli, HttpRequest* req) {
     }
     tcp_nodelay(connfd, 1);
 
-    if (req->https) {
+    if (strcmp(req->scheme.c_str(), "https") == 0) {
         hssl_ctx_t ssl_ctx = hssl_ctx_instance();
         if (ssl_ctx == NULL) {
             closesocket(connfd);
@@ -372,6 +372,7 @@ connect:
 send:
     char* data = NULL;
     size_t len  = 0;
+    bool https = strcmp(req->scheme.c_str(), "https") == 0;
     while (cli->parser->GetSendData(&data, &len)) {
         total_nsend = 0;
         while (1) {
@@ -382,7 +383,7 @@ send:
                 }
                 so_sndtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
             }
-            if (req->https) {
+            if (https) {
                 nsend = hssl_write(cli->ssl, data+total_nsend, len-total_nsend);
             }
             else {
@@ -414,7 +415,7 @@ recv:
             }
             so_rcvtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
         }
-        if (req->https) {
+        if (https) {
             nrecv = hssl_read(cli->ssl, recvbuf, sizeof(recvbuf));
         }
         else {
@@ -450,9 +451,9 @@ int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseC
     if (!cli || !req) return ERR_NULL_POINTER;
 
     if (req->url.empty() || *req->url.c_str() == '/') {
+        req->scheme = cli->https ? "https" : "http";
         req->host = cli->host;
         req->port = cli->port;
-        req->https = cli->https;
     }
 
     if (req->timeout == 0) {