Refactor proxy and socket a bit
This commit is contained in:
@ -567,7 +567,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
|
|
||||||
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) {
|
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) {
|
||||||
char buffer[CHUNK_SIZE], buf[256], *ptr;
|
char buffer[CHUNK_SIZE], buf[256], *ptr;
|
||||||
long ret = 0, len, snd_len;
|
long ret = 0, snd_len;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
snd_len = 0;
|
snd_len = 0;
|
||||||
@ -593,10 +593,9 @@ int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int
|
|||||||
}
|
}
|
||||||
ptr = buffer;
|
ptr = buffer;
|
||||||
long buf_len = ret;
|
long buf_len = ret;
|
||||||
len = sprintf(buf, "%lX\r\n", buf_len);
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
|
|
||||||
if (flags & PROXY_CHUNKED) ret = sock_send_x(client, buf, len, 0);
|
if (flags & PROXY_CHUNKED) ret = sock_send_x(client, buf, sprintf(buf, "%lX\r\n", buf_len), 0);
|
||||||
if (ret <= 0) goto err;
|
if (ret <= 0) goto err;
|
||||||
|
|
||||||
ret = sock_send_x(client, ptr, buf_len, 0);
|
ret = sock_send_x(client, ptr, buf_len, 0);
|
||||||
|
@ -163,20 +163,23 @@ long sock_recv_x(sock *s, void *buf, unsigned long len, int flags) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigned long len) {
|
long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigned long len) {
|
||||||
long ret;
|
long send_len = 0;
|
||||||
unsigned long send_len = 0;
|
for (long ret, next_len; send_len < len; send_len += ret) {
|
||||||
unsigned long next_len;
|
next_len = (long) ((buf_len < (len - send_len)) ? buf_len : (len - send_len));
|
||||||
while (send_len < len) {
|
|
||||||
next_len = (buf_len < (len - send_len)) ? buf_len : (len - send_len);
|
if ((ret = sock_recv(src, buf, next_len, MSG_WAITALL)) <= 0) {
|
||||||
ret = sock_recv(src, buf, next_len, 0);
|
if (errno == EINTR) {
|
||||||
if (ret <= 0) return -2;
|
errno = 0, ret = 0;
|
||||||
next_len = ret;
|
continue;
|
||||||
ret = sock_send(dst, buf, next_len, send_len + next_len < len ? MSG_MORE : 0);
|
} else {
|
||||||
if (ret < 0) return -1;
|
return -1;
|
||||||
if (ret != next_len) return -3;
|
|
||||||
send_len += next_len;
|
|
||||||
}
|
}
|
||||||
return (long) send_len;
|
}
|
||||||
|
|
||||||
|
if (sock_send_x(dst, buf, ret, send_len + ret < len ? MSG_MORE : 0) == -1)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return send_len;
|
||||||
}
|
}
|
||||||
|
|
||||||
long sock_splice_chunked(sock *dst, sock *src, void *buf, unsigned long buf_len, int flags) {
|
long sock_splice_chunked(sock *dst, sock *src, void *buf, unsigned long buf_len, int flags) {
|
||||||
@ -185,13 +188,15 @@ long sock_splice_chunked(sock *dst, sock *src, void *buf, unsigned long buf_len,
|
|||||||
|
|
||||||
while (!(flags & SOCK_SINGLE_CHUNK)) {
|
while (!(flags & SOCK_SINGLE_CHUNK)) {
|
||||||
ret = sock_get_chunk_header(src);
|
ret = sock_get_chunk_header(src);
|
||||||
if (ret < 0) return -2;
|
if (ret < 0) {
|
||||||
|
errno = EPROTO;
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
next_len = ret;
|
next_len = ret;
|
||||||
|
|
||||||
if (flags & SOCK_CHUNKED) {
|
if (flags & SOCK_CHUNKED) {
|
||||||
ret = sprintf(buf, "%lX\r\n", next_len);
|
if (sock_send_x(dst, buf, sprintf(buf, "%lX\r\n", next_len), 0) == -1)
|
||||||
if (sock_send_x(dst, buf, ret, 0) == -1)
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,8 +213,12 @@ long sock_splice_chunked(sock *dst, sock *src, void *buf, unsigned long buf_len,
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sock_recv_x(src, buf, 2, 0) == -1)
|
if (sock_recv_x(src, buf, 2, 0) == -1) {
|
||||||
return -1;
|
return -1;
|
||||||
|
} else if (strncmp(buf, "\r\n", 2) != 0) {
|
||||||
|
errno = EPROTO;
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (long) send_len;
|
return (long) send_len;
|
||||||
|
Reference in New Issue
Block a user