12 #define PROTO_HTTP_PRIVATE
51 char **headers_out,
size_t max_headerlen,
52 char **body_out,
size_t *body_used,
size_t max_bodylen,
56 size_t headerlen, bodylen, contentlen=0;
64 if (crlf_offset > (
int)max_headerlen ||
65 (crlf_offset < 0 &&
buf_datalen(buf) > max_headerlen)) {
66 log_debug(
LD_HTTP,
"headers too long.");
68 }
else if (crlf_offset < 0) {
69 log_debug(
LD_HTTP,
"headers not all here yet.");
74 headerlen = crlf_offset + 4;
75 size_t headers_in_chunk = 0;
76 buf_pullup(buf, headerlen, &headers, &headers_in_chunk);
79 log_debug(
LD_HTTP,
"headerlen %d, bodylen %d.", (
int)headerlen, (
int)bodylen);
81 if (max_headerlen <= headerlen) {
82 log_warn(
LD_HTTP,
"headerlen %d larger than %d. Failing.",
83 (
int)headerlen, (
int)max_headerlen-1);
86 if (max_bodylen <= bodylen) {
87 log_warn(
LD_HTTP,
"bodylen %d larger than %d. Failing.",
88 (
int)bodylen, (
int)max_bodylen-1);
94 log_warn(
LD_PROTOCOL,
"Content-Length is bogus; maybe "
95 "someone is trying to crash us.");
99 log_debug(
LD_HTTP,
"Got a contentlen of %d.",(
int)contentlen);
100 if (bodylen < contentlen) {
101 if (!force_complete) {
102 log_debug(
LD_HTTP,
"body not all here yet.");
106 if (bodylen > contentlen) {
107 bodylen = contentlen;
108 log_debug(
LD_HTTP,
"bodylen reduced to %d.",(
int)bodylen);
117 *headers_out = tor_malloc(headerlen+1);
119 (*headers_out)[headerlen] = 0;
123 *body_used = bodylen;
124 *body_out = tor_malloc(bodylen+1);
126 (*body_out)[bodylen] = 0;
142 const char *p, *newline;
143 char *len_str, *eos=NULL;
144 size_t remaining, result;
149 #define CONTENT_LENGTH "\r\nContent-Length: "
150 p = (
char*) tor_memstr(headers, headerlen, CONTENT_LENGTH);
154 tor_assert(p >= headers && p < headers+headerlen);
155 remaining = (headers+headerlen)-p;
156 p += strlen(CONTENT_LENGTH);
157 remaining -= strlen(CONTENT_LENGTH);
159 newline = memchr(p,
'\n', remaining);
163 len_str = tor_memdup_nulterm(p, newline-p);
170 *result_out = result;
int buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
size_t buf_datalen(const buf_t *buf)
int buf_peek_startswith(const buf_t *buf, const char *cmd)
int buf_get_bytes(buf_t *buf, char *string, size_t string_len)
void buf_pullup(buf_t *buf, size_t bytes, const char **head_out, size_t *len_out)
Header file for buffers.c.
Master header file for Tor-specific functionality.
uint64_t tor_parse_uint64(const char *s, int base, uint64_t min, uint64_t max, int *ok, char **next)
int fetch_from_buf_http(buf_t *buf, char **headers_out, size_t max_headerlen, char **body_out, size_t *body_used, size_t max_bodylen, int force_complete)
int peek_buf_has_http_command(const buf_t *buf)
STATIC int buf_http_find_content_length(const char *headers, size_t headerlen, size_t *result_out)
int tor_strisspace(const char *s)