Skip to content

Commit 852f697

Browse files
author
hewei.it
committed
optimize code
1 parent 03bf80f commit 852f697

File tree

5 files changed

+22
-37
lines changed

5 files changed

+22
-37
lines changed

http/HttpMessage.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,13 @@ std::string HttpMessage::Dump(bool is_dump_headers, bool is_dump_body) {
315315
}
316316

317317
void HttpRequest::DumpUrl() {
318-
if (url.size() != 0 && strncmp(url.c_str(), "http", 4) == 0) {
318+
if (url.size() != 0 && strstr(url.c_str(), "://") != NULL) {
319319
// have been complete url
320320
return;
321321
}
322322
std::string str;
323323
// scheme://
324-
str += "http";
325-
if (https) str += 's';
324+
str = scheme;
326325
str += "://";
327326
// host:port
328327
char c_str[256] = {0};
@@ -367,13 +366,13 @@ void HttpRequest::ParseUrl() {
367366
http_parser_url_init(&parser);
368367
http_parser_parse_url(url.c_str(), url.size(), 0, &parser);
369368
// scheme
370-
https = !strncmp(url.c_str(), "https", 5);
369+
scheme = url.substr(parser.field_data[UF_SCHEMA].off, parser.field_data[UF_SCHEMA].len);
371370
// host
372371
if (parser.field_set & (1<<UF_HOST)) {
373372
host = url.substr(parser.field_data[UF_HOST].off, parser.field_data[UF_HOST].len);
374373
}
375374
// port
376-
port = parser.port ? parser.port : https ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT;
375+
port = parser.port ? parser.port : strcmp(scheme.c_str(), "https") ? DEFAULT_HTTP_PORT : DEFAULT_HTTPS_PORT;
377376
// path
378377
if (parser.field_set & (1<<UF_PATH)) {
379378
path = url.c_str() + parser.field_data[UF_PATH].off;

http/HttpMessage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ class HV_EXPORT HttpRequest : public HttpMessage {
246246
// scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
247247
std::string url;
248248
// structured url
249-
bool https;
249+
std::string scheme;
250250
std::string host;
251251
int port;
252252
std::string path;
@@ -264,7 +264,7 @@ class HV_EXPORT HttpRequest : public HttpMessage {
264264
headers["User-Agent"] = DEFAULT_USER_AGENT;
265265
headers["Accept"] = "*/*";
266266
method = HTTP_GET;
267-
https = 0;
267+
scheme = "http";
268268
host = "127.0.0.1";
269269
port = DEFAULT_HTTP_PORT;
270270
path = "/";

http/client/AsyncHttpClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
4747
hio_set_peeraddr(connio, &peeraddr.sa, sockaddr_len(&peeraddr));
4848
addChannel(connio);
4949
// https
50-
if (req->https) {
50+
if (strcmp(req->scheme.c_str(), "https") == 0) {
5151
hio_enable_ssl(connio);
5252
}
5353
}

http/client/WebSocketClient.cpp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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
}

http/client/http_client.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ int http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp) {
119119
if (!cli || !req || !resp) return ERR_NULL_POINTER;
120120

121121
if (req->url.empty() || *req->url.c_str() == '/') {
122+
req->scheme = cli->https ? "https" : "http";
122123
req->host = cli->host;
123124
req->port = cli->port;
124-
req->https = cli->https;
125125
}
126126

127127
if (req->timeout == 0) {
@@ -322,7 +322,7 @@ static int __http_client_connect(http_client_t* cli, HttpRequest* req) {
322322
}
323323
tcp_nodelay(connfd, 1);
324324

325-
if (req->https) {
325+
if (strcmp(req->scheme.c_str(), "https") == 0) {
326326
hssl_ctx_t ssl_ctx = hssl_ctx_instance();
327327
if (ssl_ctx == NULL) {
328328
closesocket(connfd);
@@ -372,6 +372,7 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
372372
send:
373373
char* data = NULL;
374374
size_t len = 0;
375+
bool https = strcmp(req->scheme.c_str(), "https") == 0;
375376
while (cli->parser->GetSendData(&data, &len)) {
376377
total_nsend = 0;
377378
while (1) {
@@ -382,7 +383,7 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
382383
}
383384
so_sndtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
384385
}
385-
if (req->https) {
386+
if (https) {
386387
nsend = hssl_write(cli->ssl, data+total_nsend, len-total_nsend);
387388
}
388389
else {
@@ -414,7 +415,7 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
414415
}
415416
so_rcvtimeo(connfd, (timeout-(cur_time-start_time)) * 1000);
416417
}
417-
if (req->https) {
418+
if (https) {
418419
nrecv = hssl_read(cli->ssl, recvbuf, sizeof(recvbuf));
419420
}
420421
else {
@@ -450,9 +451,9 @@ int http_client_send_async(http_client_t* cli, HttpRequestPtr req, HttpResponseC
450451
if (!cli || !req) return ERR_NULL_POINTER;
451452

452453
if (req->url.empty() || *req->url.c_str() == '/') {
454+
req->scheme = cli->https ? "https" : "http";
453455
req->host = cli->host;
454456
req->port = cli->port;
455-
req->https = cli->https;
456457
}
457458

458459
if (req->timeout == 0) {

0 commit comments

Comments
 (0)