tpws code cleanup and simplify

This commit is contained in:
bolvan 2017-05-08 21:55:13 +03:00
parent 75cde6a1ea
commit 4331604181
5 changed files with 32 additions and 42 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -77,7 +77,6 @@ void close_tcp_conn(tproxy_conn_t *conn, struct tailhead *conn_list,
} }
static const char *http_methods[] = { "GET /","POST /","HEAD /","OPTIONS /","PUT /","DELETE /","CONNECT /","TRACE /",NULL }; static const char *http_methods[] = { "GET /","POST /","HEAD /","OPTIONS /","PUT /","DELETE /","CONNECT /","TRACE /",NULL };
static const char *http_split_host[] = { "\r\nHost: ",NULL };
#define RD_BLOCK_SIZE 8192 #define RD_BLOCK_SIZE 8192
@ -109,17 +108,18 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
{ {
if (bOutgoing) if (bOutgoing)
{ {
char buf[RD_BLOCK_SIZE + 4], *p, *pp, *phost = NULL; char buf[RD_BLOCK_SIZE + 4];
ssize_t l, method_len=0, split_pos = 0, method_split_pos = 0, host_split_pos = 0, split_array_pos_offset = 1, pos;
const char **split_array = NULL, **split_item, **method;
bool bIsHttp;
rd = recv(fd_in, buf, RD_BLOCK_SIZE, MSG_DONTWAIT); rd = recv(fd_in, buf, RD_BLOCK_SIZE, MSG_DONTWAIT);
if (rd > 0) if (rd > 0)
{ {
char *p, *pp, *pHost=NULL;
ssize_t method_len=0, split_pos=0, pos;
const char **method;
bool bIsHttp=false;
bs = rd; bs = rd;
bIsHttp = false;
for (method = http_methods; *method; method++) for (method = http_methods; *method; method++)
{ {
method_len = strlen(*method); method_len = strlen(*method);
@ -136,7 +136,6 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
if (params.unixeol) if (params.unixeol)
{ {
printf("Replacing 0D0A to 0A\n");
p = pp = buf; p = pp = buf;
while (p = find_bin(p, buf + bs - p, "\r\n", 2)) while (p = find_bin(p, buf + bs - p, "\r\n", 2))
{ {
@ -163,12 +162,18 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
*p = ' '; // insert extra space *p = ' '; // insert extra space
bs++; // block will grow by 1 byte bs++; // block will grow by 1 byte
} }
if (params.hostdot || params.hosttab)
// search for Host only if required (save some CPU)
if (params.hostdot || params.hosttab || params.hostcase || params.split_http_req==split_host)
{ {
if (phost = find_bin(buf, bs, params.unixeol ? "\nHost: " : "\r\nHost: ", params.unixeol ? 7 : 8)) // we need Host: location
pHost=find_bin(buf, bs, "\nHost: ", 7);
if (pHost) pHost++;
}
if (pHost && params.hostdot || params.hosttab)
{ {
host_split_pos = phost - buf + 7; p = pHost + 6;
p = phost + 8;
while (p < (buf + bs) && *p != '\r' && *p != '\n') p++; while (p < (buf + bs) && *p != '\r' && *p != '\n') p++;
if (p < (buf + bs)) if (p < (buf + bs))
{ {
@ -179,7 +184,7 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
bs++; // block will grow by 1 byte bs++; // block will grow by 1 byte
} }
} }
}
if (params.split_pos) if (params.split_pos)
{ {
split_pos = params.split_pos < bs ? params.split_pos : 0; split_pos = params.split_pos < bs ? params.split_pos : 0;
@ -192,36 +197,21 @@ bool handle_epollin(tproxy_conn_t *conn, int *data_transferred) {
split_pos = method_len - 1; split_pos = method_len - 1;
break; break;
case split_host: case split_host:
if (host_split_pos) if (pHost)
split_pos = host_split_pos; split_pos = pHost + 6 - buf;
else
split_array = http_split_host;
break; break;
} }
} }
if (split_array)
{
// we havent found split post yet. need to search.
for (split_item = split_array; *split_item; split_item++)
{
l = strlen(*split_item);
if (p = find_bin(buf, bs, *split_item, l))
{
split_pos = p - buf;
printf("Found split item '%s' at pos %zd. Split offset is -%zd.\n", *split_item, split_pos, split_array_pos_offset);
split_pos += l - split_array_pos_offset;
break;
}
}
}
if (params.hostcase) if (params.hostcase)
{ {
if (phost || (phost = find_bin(buf, bs, params.unixeol ? "\nHost: " : "\r\nHost: ", params.unixeol ? 7 : 8))) if (pHost)
{ {
printf("Changing 'Host:' => '%c%c%c%c:' at pos %zd\n", params.hostspell[0], params.hostspell[1], params.hostspell[2], params.hostspell[3], phost - buf + 2); printf("Changing 'Host:' => '%c%c%c%c:' at pos %zd\n", params.hostspell[0], params.hostspell[1], params.hostspell[2], params.hostspell[3], pHost - buf);
memcpy(phost + 2, params.hostspell, 4); memcpy(pHost, params.hostspell, 4);
} }
} }
if (params.methodeol) if (params.methodeol)
{ {
printf("Adding EOL before method\n"); printf("Adding EOL before method\n");