Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kehengzhong authored Jan 28, 2021
1 parent d9290c9 commit f649403
Show file tree
Hide file tree
Showing 12 changed files with 372 additions and 210 deletions.
8 changes: 4 additions & 4 deletions src/http_cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int http_cli_recv_cc (void * vcon)

msg = http_con_msg_first(pcon);
if (msg && msg->proxied == 1 && (srvmsg = msg->proxymsg) &&
frameL(srvmsg->req_body_stream) >= mgmt->proxy_buffer_size)
chunk_rest_size(srvmsg->req_body_chunk, 0) >= mgmt->proxy_buffer_size)
{
/* congestion control: by neglecting the read-ready event,
underlying TCP stack recv-buffer will be full soon.
Expand Down Expand Up @@ -159,7 +159,7 @@ int http_cli_send_cc (void * vcon)
srvcon = srvmsg->pcon;

if (srvcon && srvcon->read_ignored > 0 &&
frameL(msg->res_body_stream) < mgmt->proxy_buffer_size)
chunk_rest_size(msg->res_body_chunk, 0) < mgmt->proxy_buffer_size)
{
iodev_add_notify(srvcon->pdev, RWF_READ);
srvcon->read_ignored = 0;
Expand Down Expand Up @@ -230,7 +230,7 @@ int http_srv_recv_cc (void * vcon)

msg = http_con_msg_first(pcon);
if (msg && msg->proxied == 2 && (climsg = msg->proxymsg) &&
frameL(climsg->res_body_stream) >= mgmt->proxy_buffer_size)
chunk_rest_size(climsg->res_body_chunk, 0) >= mgmt->proxy_buffer_size)
{
/* congestion control: by neglecting the read-ready event,
underlying TCP stack recv-buffer will be full soon.
Expand Down Expand Up @@ -295,7 +295,7 @@ int http_srv_send_cc (void * vcon)
clicon = climsg->pcon;

if (clicon && clicon->read_ignored > 0 &&
frameL(msg->req_body_stream) < mgmt->proxy_buffer_size)
chunk_rest_size(msg->req_body_chunk, 0) < mgmt->proxy_buffer_size)
{
iodev_add_notify(clicon->pdev, RWF_READ);
clicon->read_ignored = 0;
Expand Down
125 changes: 60 additions & 65 deletions src/http_cli_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,8 @@ int http_cli_recv_parse (void * vcon)
int64 hdrlen = 0;
uint8 * pbyte = NULL;
uint8 * pbgn = NULL;
char url[2048];

HTTPMsg * proxymsg = NULL;

FcgiSrv * cgisrv = NULL;
FcgiMsg * cgimsg = NULL;

if (!pcon) return -1;
Expand Down Expand Up @@ -392,76 +389,64 @@ int http_cli_recv_parse (void * vcon)
pcon->rcv_state = HTTP_CON_READY;
}

/* check the request if it's to be proxyed to other origin server */
if (http_proxy_check(msg, url, sizeof(url)-1)) {

if (http_proxy_cache_open(msg) >= 3) {
/* cache file exists in local directory */
goto proxy_end;
}

/* create one proxy HTTPMsg object, with headers X-Forwarded-For and X-Real-IP */
proxymsg = msg->proxymsg = http_proxy_srvmsg_open(msg, url, strlen(url));
if (proxymsg == NULL) {
goto proxy_end;
}

msg->proxied = 1;

http_proxy_srv_send_start(proxymsg);

if (http_proxy_handle(msg) >= 0)
return 0;
}
proxy_end:

/* check the request if it's to be transformed to FCGI server */
if (http_fcgi_check(msg, url, sizeof(url)-1)) {
msg->fastcgi = 1;

cgisrv = http_fcgisrv_open(mgmt, url, 100);
if (!cgisrv) return -130;

http_fcgi_send_start(cgisrv, msg);

if (http_fcgi_handle(msg) >= 0)
return 0;
}

/* HTTP POST/PUT request body may be encoded as following enctype:
(1) application/x-www-form-urlencoded
(2) multipart/form-data
(3) application/json
(4) text/xml
(5) application/octet-stream
*/

switch (msg->req_body_flag) {
case BC_CONTENT_LENGTH:
case BC_TE:
ret = http_cli_reqbody_parse(pcon, msg);
if (ret < 0) {
return -107;
} else if (ret == 0) { //waiting more body
pcon->rcv_state = HTTP_CON_WAITING_BODY;
} else {
pcon->rcv_state = HTTP_CON_READY;

return 1;
}
break;

return http_reqbody_handle(msg);
}

case BC_TE_INVALID:
case BC_UNKNOWN:
return -108;
return 0;
}

case BC_NONE:
case BC_TUNNEL:
default:
int http_reqbody_handle (void * vmsg)
{
HTTPMsg * msg = (HTTPMsg *)vmsg;
HTTPCon * pcon = NULL;
int ret = 0;

if (!msg) return -1;

pcon = (HTTPCon *)msg->pcon;
if (!pcon) return -2;

/* HTTP POST/PUT request body may be encoded as following enctype:
(1) application/x-www-form-urlencoded
(2) multipart/form-data
(3) application/json
(4) text/xml
(5) application/octet-stream
*/

switch (msg->req_body_flag) {
case BC_CONTENT_LENGTH:
case BC_TE:
ret = http_cli_reqbody_parse(pcon, msg);
if (ret < 0) {
return -107;
} else if (ret == 0) { //waiting more body
pcon->rcv_state = HTTP_CON_WAITING_BODY;
} else {
pcon->rcv_state = HTTP_CON_READY;
return 2;
break;

return 1;
}
break;

case BC_TE_INVALID:
case BC_UNKNOWN:
return -108;

case BC_NONE:
case BC_TUNNEL:
default:
pcon->rcv_state = HTTP_CON_READY;
return 2;
break;
}

return 0;
}

Expand Down Expand Up @@ -666,6 +651,7 @@ int http_cli_send (void * vcon)
int num = 0;
int err = 0;
uint8 shutdown = 0;
uint8 closecon = 0;

if (!pcon) return -1;

Expand Down Expand Up @@ -795,6 +781,9 @@ int http_cli_send (void * vcon)
}

if (chunk_get_end(chunk, msg->res_stream_sent, httpchunk) == 1) {
if (msg->res_status >= 400)
closecon++;

/* send response to client successfully */
http_con_msg_del(pcon, msg);
http_msg_close(msg);
Expand All @@ -812,6 +801,12 @@ int http_cli_send (void * vcon)

} //end while

if (closecon) {
pcon->snd_state = HTTP_CON_IDLE;
http_cli_con_crash(pcon, 1);
return ret;
}

pcon->snd_state = HTTP_CON_SEND_READY;

/* the response has been sent to client. the current HTTPCon
Expand Down
3 changes: 3 additions & 0 deletions src/http_con.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ int http_con_connect (void * vpcon)
return 0;
}

tolog(1, "eJet - TCP Connect: failed to connecting to '%s:%d'.\n",
pcon->dstip, pcon->dstport);

if (pcon->pdev) {
iodev_close(pcon->pdev);
pcon->pdev = NULL;
Expand Down
84 changes: 29 additions & 55 deletions src/http_do.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,57 @@ static char * hdr_accept = "text/html,application/xhtml+xml,application/xml;q=0.
static char * hdr_accept_charset = "utf-8, iso-8859-1, utf-16, *;q=0.7";
static char * hdr_accept_lang = "zh-CN, en-US";


int http_redirect_request (void * vmsg)
{
HTTPMsg * msg = (HTTPMsg *)vmsg;
HTTPMgmt * mgmt = NULL;
HTTPSrv * srv = NULL;
HTTPCon * pcon = NULL;
char * p = NULL;
int len = 0;
char url[4096];
HTTPMsg * msg = (HTTPMsg *)vmsg;
HTTPCon * pcon = NULL;
char * p = NULL;
int len = 0;
frame_p uri;

if (!msg) return -1;

mgmt = (HTTPMgmt *)msg->httpmgmt;
if (!mgmt) return -2;

if (++msg->redirecttimes >= 6) {
msg->SetStatus(msg, 417, NULL);
msg->DelResHdr(msg, "Location", 8);
msg->DelResHdr(msg, "Content-Length", -1);
msg->DelResHdr(msg, "Transfer-Encoding", -1);
msg->DelResHdr(msg, "Content-Type", -1);

tolog(1, "eJet - Redirect: HTTP auto-redirect '%s' too many times.\n",
http_uri_string(msg->uri));

return -100;
}

/* 301/302 response may redirect to another origin server.
msg->pcon is connected to original server and not used to send the msg */

msg->GetResHdrP(msg, "Location", 8, &p, &len);
if (!p || len < 1) return -100;
if (!p || len < 1) {
tolog(1, "eJet - Redirect: invalid Location returned from request '%s'.\n",
http_uri_string(msg->uri));

return -100;
}

if (strncasecmp(p, "http://", 7) != 0 &&
strncasecmp(p, "https://", 8) != 0 &&
*p != '/')
{
str_secpy(url, sizeof(url)-1, msg->uri->baseuri, msg->uri->baseurilen);
if (url[strlen(url) - 1] != '/')
str_secpy(url + strlen(url), sizeof(url) - 1 - strlen(url), "/", 1);
str_secpy(url + strlen(url), sizeof(url) - 1 - strlen(url), p, len);
uri = frame_new(512);
frame_put_nlast(uri, msg->uri->baseuri, msg->uri->baseurilen);
if (frame_read(uri, frameL(uri)-1) != '/')
frame_put_last(uri, '/');
frame_put_nlast(uri, p, len);

msg->SetURL(msg, url, strlen(url), 1);
msg->SetURL(msg, frameP(uri), frameL(uri), 1);
frame_free(uri);
} else {
msg->SetURL(msg, p, len, 1);
}

sock_addr_get(msg->req_host, msg->req_hostlen, msg->req_port, 0,
msg->dstip, &msg->dstport, NULL);
msg->dstport = msg->req_port;

/* the original Cookie should be removed before encoding */
Expand Down Expand Up @@ -92,40 +96,23 @@ int http_redirect_request (void * vmsg)

chunk_set_end(msg->req_body_chunk);

/* now bind the HTTPMsg to one HTTPCon allocated by HTTPSrv, and start sending it */
srv = http_srv_open(mgmt, msg->dstip, msg->dstport, msg->ssl_link, 15);
if (!srv) {
if (http_srv_msg_dns(msg, http_srv_msg_dns_cb) < 0) {
http_msg_close(msg);
return -200;
}

pcon = http_srv_connect(srv);
if (!pcon) {
http_srv_msg_push(srv, msg);
} else {
http_con_msg_add(pcon, msg);

http_srv_send(pcon);
}

return 0;
}


int do_http_request (void * vmsg)
{
HTTPMsg * msg = (HTTPMsg *)vmsg;
HTTPMgmt * mgmt = NULL;
HTTPSrv * srv = NULL;
HTTPCon * pcon = NULL;
char * fname = NULL;
char * mime = NULL;
HTTPMsg * msg = (HTTPMsg *)vmsg;
char * fname = NULL;
char * mime = NULL;

if (!msg) return -1;

mgmt = (HTTPMgmt *)msg->httpmgmt;
if (!mgmt) return -2;

if (msg->req_body_flag == BC_CONTENT_LENGTH) {

if (http_header_get(msg, 0, "Content-Type", -1) == NULL) {
Expand All @@ -146,20 +133,10 @@ int do_http_request (void * vmsg)

chunk_set_end(msg->req_body_chunk);

/* now bind the HTTPMsg to one HTTPCon allocated by HTTPSrv, and start sending it */
srv = http_srv_open(mgmt, msg->dstip, msg->dstport, msg->ssl_link, 50);
if (!srv) {
if (http_srv_msg_dns(msg, http_srv_msg_dns_cb) < 0) {
http_msg_close(msg);
return -100;
}

pcon = http_srv_connect(srv);
if (!pcon) {
http_srv_msg_push(srv, msg);
} else {
http_con_msg_add(pcon, msg);
http_srv_send(pcon);
}

return 0;
}
Expand All @@ -183,8 +160,6 @@ void * do_http_get_msg (void * vmgmt, char * url, int urllen,

msg->req_body_flag = BC_NONE;

sock_addr_get(msg->req_host, msg->req_hostlen, msg->req_port, 0,
msg->dstip, &msg->dstport, NULL);
msg->dstport = msg->req_port;

msg->SetResponseHandle(msg, resfunc, para, cbval, resfile, resoff, rcvprocfunc, funcpara);
Expand Down Expand Up @@ -251,8 +226,7 @@ void * do_http_post_msg (void * vmgmt, char * url, int urllen, char * mime,

msg->req_body_flag = BC_CONTENT_LENGTH;

sock_addr_get(msg->req_host, msg->req_hostlen, msg->req_port, 0,
msg->dstip, &msg->dstport, NULL);
msg->dstport = msg->req_port;

msg->SetResponseHandle(msg, resfunc, para, cbval, resfile, resoff, rcvprocfunc, rcvpara);

Expand Down
7 changes: 7 additions & 0 deletions src/http_fcgi_con.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ int http_fcgicon_connect (void * vpcon)
return 0;
}

if (pcon->socktype == 0)
tolog(1, "eJet - FastCGI Connect: failed to build TCP Connection to server '%s:%d'.\n",
pcon->dstip, pcon->dstport);
else
tolog(1, "eJet - FastCGI Connect: failed to build Unix Socket to server '%s'.\n",
pcon->unixsock);

if (pcon->pdev) {
iodev_close(pcon->pdev);
pcon->pdev = NULL;
Expand Down
Loading

0 comments on commit f649403

Please sign in to comment.