11#include " WebSocketClient.h"
22
3- #include " http_parser.h" // for http_parser_url
43#include " base64.h"
54#include " hlog.h"
65
@@ -35,28 +34,18 @@ int WebSocketClient::open(const char* _url) {
3534 }
3635 }
3736 hlogi (" %s" , url.c_str ());
38- http_parser_url parser;
39- http_parser_url_init (&parser);
40- http_parser_parse_url (url.c_str (), url.size (), 0 , &parser);
41- // scheme
42- bool wss = !strncmp (url.c_str (), " wss" , 3 );
43- // host
44- std::string host = " 127.0.0.1" ;
45- if (parser.field_set & (1 <<UF_HOST)) {
46- host = url.substr (parser.field_data [UF_HOST].off , parser.field_data [UF_HOST].len );
47- }
48- // port
49- int port = parser.port ? parser.port : wss ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
50- // path
51- std::string path = " /" ;
52- if (parser.field_set & (1 <<UF_PATH)) {
53- path = url.c_str () + parser.field_data [UF_PATH].off ;
54- }
37+ http_req_.reset (new HttpRequest);
38+ // ws => http
39+ http_req_->url = " http" + url.substr (2 , -1 );
40+ http_req_->ParseUrl ();
5541
56- int connfd = createsocket (port, host.c_str ());
42+ int connfd = createsocket (http_req_-> port , http_req_-> host .c_str ());
5743 if (connfd < 0 ) {
5844 return connfd;
5945 }
46+
47+ // wss
48+ bool wss = strncmp (url.c_str (), " wss" , 3 ) == 0 ;
6049 if (wss) {
6150 withTLS ();
6251 }
@@ -65,10 +54,6 @@ int WebSocketClient::open(const char* _url) {
6554 if (channel->isConnected ()) {
6655 state = CONNECTED;
6756 // websocket_handshake
68- http_req_.reset (new HttpRequest);
69- http_req_->method = HTTP_GET;
70- // ws => http
71- http_req_->url = " http" + url.substr (2 , -1 );
7257 http_req_->headers [" Connection" ] = " Upgrade" ;
7358 http_req_->headers [" Upgrade" ] = " websocket" ;
7459 // generate SEC_WEBSOCKET_KEY
@@ -105,7 +90,7 @@ int WebSocketClient::open(const char* _url) {
10590 }
10691 if (http_parser_->IsComplete ()) {
10792 if (http_resp_->status_code != HTTP_STATUS_SWITCHING_PROTOCOLS) {
108- hloge (" server side not support websockt !" );
93+ hloge (" server side not support websocket !" );
10994 channel->close ();
11095 return ;
11196 }
0 commit comments