summaryrefslogtreecommitdiff
path: root/ot_http.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_http.c')
-rw-r--r--ot_http.c53
1 files changed, 12 insertions, 41 deletions
diff --git a/ot_http.c b/ot_http.c
index 4c4121b..4675efb 100644
--- a/ot_http.c
+++ b/ot_http.c
@@ -15,7 +15,7 @@
15#include <unistd.h> 15#include <unistd.h>
16 16
17/* Libowfat */ 17/* Libowfat */
18#include "iob.h" 18#include "io.h"
19 19
20/* Opentracker */ 20/* Opentracker */
21#include "ot_accesslist.h" 21#include "ot_accesslist.h"
@@ -29,14 +29,7 @@
29#include "scan_urlencoded_query.h" 29#include "scan_urlencoded_query.h"
30#include "trackerlogic.h" 30#include "trackerlogic.h"
31 31
32#ifdef WANT_NO_AUTO_FREE
33#define OT_IOB_INIT(B) (bzero(B, sizeof(io_batch)), 0)
34#else
35#define OT_IOB_INIT(B) iob_init_autofree(B, 0)
36#endif
37
38#define OT_MAXMULTISCRAPE_COUNT 64 32#define OT_MAXMULTISCRAPE_COUNT 64
39#define OT_BATCH_LIMIT (1024 * 1024 * 16)
40extern const char *g_redirecturl; 33extern const char *g_redirecturl;
41 34
42const char *g_stats_path; 35const char *g_stats_path;
@@ -90,20 +83,17 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) {
90 83
91 memcpy(outbuf, ws->reply + written_size, ws->reply_size - written_size); 84 memcpy(outbuf, ws->reply + written_size, ws->reply_size - written_size);
92 if (!cookie->batch) { 85 if (!cookie->batch) {
93 cookie->batch = malloc(sizeof(io_batch)); 86 cookie->batch = calloc(1, sizeof(ot_batch));
94 if (!cookie->batch || OT_IOB_INIT(cookie->batch) == -1) { 87 if (!cookie->batch) {
95 free(cookie->batch);
96 free(outbuf); 88 free(outbuf);
97 buf_reset(&cookie->request); 89 buf_reset(&cookie->request);
98 free(cookie); 90 free(cookie);
99 io_close(sock); 91 io_close(sock);
100 return; 92 return;
101 } 93 }
102
103 cookie->batches = 1;
104 } 94 }
105 95
106 iob_addbuf_free(cookie->batch, outbuf, ws->reply_size - written_size); 96 batch_add(cookie->batch, outbuf, ws->reply_size - written_size);
107 97
108 /* writeable short data sockets just have a tcp timeout */ 98 /* writeable short data sockets just have a tcp timeout */
109 if (!ws->keep_alive) { 99 if (!ws->keep_alive) {
@@ -146,10 +136,9 @@ ssize_t http_issue_error(const int64_t sock, struct ot_workstruct *ws, int code)
146 136
147ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t iovec_entries, struct iovec *iovector, int is_partial) { 137ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t iovec_entries, struct iovec *iovector, int is_partial) {
148 struct http_data *cookie = io_getcookie(sock); 138 struct http_data *cookie = io_getcookie(sock);
149 io_batch *current;
150 char *header; 139 char *header;
151 const char *encoding = ""; 140 const char *encoding = "";
152 size_t i, header_size, size = iovec_length(&iovec_entries, (const struct iovec **)&iovector); 141 size_t header_size, size = iovec_length(&iovec_entries, (const struct iovec **)&iovector);
153 tai6464 t; 142 tai6464 t;
154 143
155 /* No cookie? Bad socket. Leave. */ 144 /* No cookie? Bad socket. Leave. */
@@ -199,42 +188,24 @@ ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t
199 } 188 }
200 189
201 if (!cookie->batch) { 190 if (!cookie->batch) {
202 cookie->batch = malloc(sizeof(io_batch)); 191 cookie->batch = calloc(1, sizeof(ot_batch));
203 if (!cookie->batch || OT_IOB_INIT(cookie->batch) == -1) { 192 if (!cookie->batch) {
204 free(cookie->batch);
205 free(header); 193 free(header);
206 iovec_free(&iovec_entries, &iovector); 194 iovec_free(&iovec_entries, &iovector);
207 HTTPERROR_500; 195 HTTPERROR_500;
208 } 196 }
209 cookie->batches = 1;
210 } 197 }
211 current = cookie->batch + cookie->batches - 1;
212 iob_addbuf_free(current, header, header_size);
213
214 /* Split huge iovectors into separate io_batches */
215 for (i = 0; i < (size_t)iovec_entries; ++i) {
216 /* If the current batch's limit is reached, try to reallocate a new batch to work on */
217 if (current->bytesleft > OT_BATCH_LIMIT) {
218 io_batch *new_batch = realloc(cookie->batch, (cookie->batches + 1) * sizeof(io_batch));
219 198
220 if (new_batch) { 199 batch_add(cookie->batch, header, header_size);
221 cookie->batch = new_batch; 200 batch_add_vector(cookie->batch, iovector, iovec_entries);
222 if (OT_IOB_INIT(cookie->batch + cookie->batches) != -1)
223 ++cookie->batches;
224 }
225 /* realloc may have moved the batches, so current is stale either way */
226 current = cookie->batch + cookie->batches - 1;
227 }
228 iob_addbuf_free(current, iovector[i].iov_base, iovector[i].iov_len);
229 }
230 free(iovector); 201 free(iovector);
202
231 if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) 203 if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)
232 iob_addbuf(current, "\r\n", 2); 204 batch_add_copy(cookie->batch, "\r\n", 2);
233 } 205 }
234 206
235 if ((cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) && cookie->batch && !is_partial) { 207 if ((cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) && cookie->batch && !is_partial) {
236 current = cookie->batch + cookie->batches - 1; 208 batch_add_copy(cookie->batch, "0\r\n\r\n", 5);
237 iob_addbuf(current, "0\r\n\r\n", 5);
238 cookie->flag &= ~STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER; 209 cookie->flag &= ~STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER;
239 } 210 }
240 211