mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-28 20:58:31 +00:00
Reduce stack usage when parsing HTTP URL
PUBLISHED_FROM=682f59e58d8335352cc09987a91e85cc264cf0f8
This commit is contained in:
1 parent
b97d3fea4b
commit
799cc369e0
1 file changed
+32
-14
+32
-14
@@ -5969,9 +5969,10 @@ struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
|
|||||||
const char *url,
|
const char *url,
|
||||||
const char *extra_headers,
|
const char *extra_headers,
|
||||||
const char *post_data) {
|
const char *post_data) {
|
||||||
struct mg_connection *nc;
|
struct mg_connection *nc = NULL;
|
||||||
char addr[1100], path[4096]; /* NOTE: keep sizes in sync with sscanf below */
|
char *addr = NULL;
|
||||||
int use_ssl = 0, addr_len = 0;
|
const char *path = NULL;
|
||||||
|
int use_ssl = 0, addr_len = 0, port_i = -1;
|
||||||
|
|
||||||
if (memcmp(url, "http://", 7) == 0) {
|
if (memcmp(url, "http://", 7) == 0) {
|
||||||
url += 7;
|
url += 7;
|
||||||
@@ -5983,15 +5984,32 @@ struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
addr[0] = path[0] = '\0';
|
while (*url != '\0') {
|
||||||
|
addr = (char *) MG_REALLOC(addr, addr_len + 5 /* space for port too. */);
|
||||||
/* addr buffer size made smaller to allow for port to be prepended */
|
if (addr == NULL) {
|
||||||
sscanf(url, "%1095[^/]/%4095s", addr, path);
|
DBG(("OOM"));
|
||||||
if (strchr(addr, ':') == NULL) {
|
return NULL;
|
||||||
addr_len = strlen(addr);
|
}
|
||||||
strncat(addr, use_ssl ? ":443" : ":80", sizeof(addr) - (addr_len + 1));
|
if (*url == '/') {
|
||||||
|
url++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (*url == ':') port_i = addr_len;
|
||||||
|
addr[addr_len++] = *url;
|
||||||
|
addr[addr_len] = '\0';
|
||||||
|
url++;
|
||||||
|
}
|
||||||
|
if (addr_len == 0) goto cleanup;
|
||||||
|
if (port_i < 0) {
|
||||||
|
port_i = addr_len;
|
||||||
|
strcpy(addr + port_i, use_ssl ? ":443" : ":80");
|
||||||
|
} else {
|
||||||
|
port_i = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (path == NULL) path = url;
|
||||||
|
|
||||||
|
DBG(("%s %s", addr, path));
|
||||||
if ((nc = mg_connect(mgr, addr, ev_handler)) != NULL) {
|
if ((nc = mg_connect(mgr, addr, ev_handler)) != NULL) {
|
||||||
mg_set_protocol_http_websocket(nc);
|
mg_set_protocol_http_websocket(nc);
|
||||||
|
|
||||||
@@ -6001,10 +6019,8 @@ struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addr_len) {
|
/* If the port was addred by us, restore the original host. */
|
||||||
/* Do not add port. See https://github.com/cesanta/mongoose/pull/304 */
|
if (port_i >= 0) addr[port_i] = '\0';
|
||||||
addr[addr_len] = '\0';
|
|
||||||
}
|
|
||||||
mg_printf(nc, "%s /%s HTTP/1.1\r\nHost: %s\r\nContent-Length: %" SIZE_T_FMT
|
mg_printf(nc, "%s /%s HTTP/1.1\r\nHost: %s\r\nContent-Length: %" SIZE_T_FMT
|
||||||
"\r\n%s\r\n%s",
|
"\r\n%s\r\n%s",
|
||||||
post_data == NULL ? "GET" : "POST", path, addr,
|
post_data == NULL ? "GET" : "POST", path, addr,
|
||||||
@@ -6013,6 +6029,8 @@ struct mg_connection *mg_connect_http(struct mg_mgr *mgr,
|
|||||||
post_data == NULL ? "" : post_data);
|
post_data == NULL ? "" : post_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
MG_FREE(addr);
|
||||||
return nc;
|
return nc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user