2 Unix SMB/CIFS implementation.
6 Copyright (C) 2013 Samuel Cabrero <samuelcabrero@kernevil.me>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/util/tevent_ntstatus.h"
25 #include "http_internal.h"
26 #include "util/tevent_werror.h"
27 #include "lib/util/dlinklist.h"
31 * Determines if a response should have a body.
32 * @return 1 if the response MUST have a body; 0 if the response MUST NOT have
33 * a body. Returns -1 on error.
35 static int http_response_needs_body(struct http_request *req)
37 struct http_header *h = NULL;
41 for (h = req->headers; h != NULL; h = h->next) {
47 cmp = strcasecmp(h->key, "Content-Length");
52 n = sscanf(h->value, "%llu%c", &v, &c);
57 req->remaining_content_length = v;
71 * Parses the HTTP headers
73 static enum http_read_status http_parse_headers(struct http_read_response_state *state)
75 enum http_read_status status = HTTP_ALL_DATA_READ;
84 if (!state || !state->response) {
85 DEBUG(0, ("%s: Invalid Parameter\n", __func__));
86 return HTTP_DATA_CORRUPTED;
89 if (state->buffer.length > state->max_headers_size) {
90 DEBUG(0, ("%s: Headers too long: %zi, maximum length is %zi\n", __func__,
91 state->buffer.length, state->max_headers_size));
92 return HTTP_DATA_TOO_LONG;
95 line = talloc_strndup(state, (char *)state->buffer.data, state->buffer.length);
97 DEBUG(0, ("%s: Memory error\n", __func__));
98 return HTTP_DATA_CORRUPTED;
101 ptr = strstr(line, "\r\n");
104 return HTTP_MORE_DATA_EXPECTED;
107 state->response->headers_size += state->buffer.length;
109 if (strncmp(line, "\r\n", 2) == 0) {
110 DEBUG(11,("%s: All headers read\n", __func__));
112 ret = http_response_needs_body(state->response);
115 if (state->response->remaining_content_length <= state->max_content_length) {
116 DEBUG(11, ("%s: Start of read body\n", __func__));
117 state->parser_state = HTTP_READING_BODY;
122 DEBUG(11, ("%s: Skipping body for code %d\n", __func__,
123 state->response->response_code));
124 state->parser_state = HTTP_READING_DONE;
127 DEBUG(0, ("%s_: Error in http_response_needs_body\n", __func__));
129 return HTTP_DATA_CORRUPTED;
134 return HTTP_ALL_DATA_READ;
137 n = sscanf(line, "%m[^:]: %m[^\r\n]\r\n", &key, &value);
139 DEBUG(0, ("%s: Error parsing header '%s'\n", __func__, line));
140 status = HTTP_DATA_CORRUPTED;
144 if (http_add_header(state->response, &state->response->headers, key, value) == -1) {
145 DEBUG(0, ("%s: Error adding header\n", __func__));
146 status = HTTP_DATA_CORRUPTED;
158 * Parses the first line of a HTTP response
160 static bool http_parse_response_line(struct http_read_response_state *state)
173 DEBUG(0, ("%s: Input parameter is NULL\n", __func__));
177 line = talloc_strndup(state, (char*)state->buffer.data, state->buffer.length);
179 DEBUG(0, ("%s: Memory error\n", __func__));
183 n = sscanf(line, "%m[^/]/%c.%c %d %m[^\r\n]\r\n",
184 &protocol, &major, &minor, &code, &msg);
186 DEBUG(11, ("%s: Header parsed(%i): protocol->%s, major->%c, minor->%c, "
187 "code->%d, message->%s\n", __func__, n, protocol, major, minor,
191 DEBUG(0, ("%s: Error parsing header\n", __func__));
197 DEBUG(0, ("%s: Bad HTTP major number '%c'\n", __func__, major));
203 DEBUG(0, ("%s: Bad response code '%d'", __func__, code));
209 DEBUG(0, ("%s: Error parsing HTTP data\n", __func__));
214 state->response->major = major;
215 state->response->minor = minor;
216 state->response->response_code = code;
217 state->response->response_code_line = talloc_strndup(state->response,
228 * Parses header lines from a request or a response into the specified
229 * request object given a buffer.
232 * HTTP_DATA_CORRUPTED on error
233 * HTTP_MORE_DATA_EXPECTED when we need to read more headers
234 * HTTP_DATA_TOO_LONG on error
235 * HTTP_ALL_DATA_READ when all headers have been read
237 static enum http_read_status http_parse_firstline(struct http_read_response_state *state)
239 enum http_read_status status = HTTP_ALL_DATA_READ;
245 DEBUG(0, ("%s: Invalid Parameter\n", __func__));
246 return HTTP_DATA_CORRUPTED;
249 if (state->buffer.length > state->max_headers_size) {
250 DEBUG(0, ("%s: Headers too long: %zi, maximum length is %zi\n", __func__,
251 state->buffer.length, state->max_headers_size));
252 return HTTP_DATA_TOO_LONG;
255 line = talloc_strndup(state, (char *)state->buffer.data, state->buffer.length);
257 DEBUG(0, ("%s: Not enough memory\n", __func__));
258 return HTTP_DATA_CORRUPTED;
261 ptr = strstr(line, "\r\n");
264 return HTTP_MORE_DATA_EXPECTED;
267 state->response->headers_size = state->buffer.length;
268 if (!http_parse_response_line(state)) {
269 status = HTTP_DATA_CORRUPTED;
272 /* Next state, read HTTP headers */
273 state->parser_state = HTTP_READING_HEADERS;
279 static enum http_read_status http_read_body(struct http_read_response_state *state)
281 struct http_request *resp = state->response;
283 if (state->buffer.length < resp->remaining_content_length) {
284 return HTTP_MORE_DATA_EXPECTED;
287 resp->body = state->buffer;
288 state->buffer = data_blob_null;
289 talloc_steal(resp, resp->body.data);
290 resp->remaining_content_length = 0;
292 state->parser_state = HTTP_READING_DONE;
293 return HTTP_ALL_DATA_READ;
296 static enum http_read_status http_read_trailer(struct http_read_response_state *state)
298 enum http_read_status status = HTTP_DATA_CORRUPTED;
303 static enum http_read_status http_parse_buffer(struct http_read_response_state *state)
306 DEBUG(0, ("%s: Invalid parameter\n", __func__));
307 return HTTP_DATA_CORRUPTED;
310 switch (state->parser_state) {
311 case HTTP_READING_FIRSTLINE:
312 return http_parse_firstline(state);
313 case HTTP_READING_HEADERS:
314 return http_parse_headers(state);
315 case HTTP_READING_BODY:
316 return http_read_body(state);
318 case HTTP_READING_TRAILER:
319 return http_read_trailer(state);
321 case HTTP_READING_DONE:
323 return HTTP_ALL_DATA_READ;
325 DEBUG(0, ("%s: Illegal parser state %d", __func__,
326 state->parser_state));
329 return HTTP_DATA_CORRUPTED;
332 static int http_header_is_valid_value(const char *value)
334 const char *p = NULL;
338 DEBUG(0, ("%s: Invalid parameter\n", __func__));
343 while ((p = strpbrk(p, "\r\n")) != NULL) {
344 /* Expect only one new line */
345 p += strspn(p, "\r\n");
346 /* Expect a space or tab for continuation */
347 if (*p != ' ' && *p != '\t')
353 static int http_add_header_internal(TALLOC_CTX *mem_ctx,
354 struct http_header **headers,
355 const char *key, const char *value,
358 struct http_header *tail = NULL;
359 struct http_header *h = NULL;
362 if (!headers || !key || !value) {
363 DEBUG(0, ("Invalid parameter\n"));
370 for (h = *headers; h != NULL; h = h->next) {
371 if (strcasecmp(key, h->key) == 0) {
377 /* Replace header value */
379 talloc_free(h->value);
381 h->value = talloc_strdup(h, value);
382 DEBUG(11, ("%s: Replaced HTTP header: key '%s', value '%s'\n",
383 __func__, h->key, h->value));
389 h = talloc(mem_ctx, struct http_header);
390 h->key = talloc_strdup(h, key);
391 h->value = talloc_strdup(h, value);
392 DLIST_ADD_END(*headers, h);
393 tail = DLIST_TAIL(*headers);
395 DEBUG(0, ("%s: Error adding header\n", __func__));
398 DEBUG(11, ("%s: Added HTTP header: key '%s', value '%s'\n",
399 __func__, h->key, h->value));
403 int http_add_header(TALLOC_CTX *mem_ctx,
404 struct http_header **headers,
405 const char *key, const char *value)
407 if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
408 DEBUG(0, ("%s: Dropping illegal header key\n", __func__));
412 if (!http_header_is_valid_value(value)) {
413 DEBUG(0, ("%s: Dropping illegal header value\n", __func__));
417 return (http_add_header_internal(mem_ctx, headers, key, value, false));
420 int http_replace_header(TALLOC_CTX *mem_ctx,
421 struct http_header **headers,
422 const char *key, const char *value)
424 if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
425 DEBUG(0, ("%s: Dropping illegal header key\n", __func__));
429 if (!http_header_is_valid_value(value)) {
430 DEBUG(0, ("%s: Dropping illegal header value\n", __func__));
434 return (http_add_header_internal(mem_ctx, headers, key, value, true));
438 * Remove a header from the headers list.
440 * Returns 0, if the header was successfully removed.
441 * Returns -1, if the header could not be found.
443 int http_remove_header(struct http_header **headers, const char *key)
445 struct http_header *header;
448 if (!headers || !key) {
449 DEBUG(0, ("%s: Invalid parameter\n", __func__));
453 for(header = *headers; header != NULL; header = header->next) {
454 if (strcmp(key, header->key) == 0) {
455 DLIST_REMOVE(*headers, header);
462 static int http_read_response_next_vector(struct tstream_context *stream,
465 struct iovec **_vector,
468 struct http_read_response_state *state;
469 struct iovec *vector;
472 if (!stream || !private_data || !_vector || !_count) {
473 DEBUG(0, ("%s: Invalid Parameter\n", __func__));
476 state = talloc_get_type_abort(private_data, struct http_read_response_state);
477 vector = talloc_array(mem_ctx, struct iovec, 1);
479 DEBUG(0, ("%s: No more memory\n", __func__));
483 if (state->buffer.data == NULL) {
484 /* Allocate buffer */
485 state->buffer.data = talloc_zero_array(state, uint8_t, 1);
486 if (!state->buffer.data) {
487 DEBUG(0, ("%s: No more memory\n", __func__));
490 state->buffer.length = 1;
492 /* Return now, nothing to parse yet */
493 vector[0].iov_base = (void *)(state->buffer.data);
494 vector[0].iov_len = 1;
500 switch (http_parse_buffer(state)) {
501 case HTTP_ALL_DATA_READ:
502 if (state->parser_state == HTTP_READING_DONE) {
503 /* Full request or response parsed */
507 /* Free current buffer and allocate new one */
508 TALLOC_FREE(state->buffer.data);
509 state->buffer.data = talloc_zero_array(state, uint8_t, 1);
510 if (!state->buffer.data) {
513 state->buffer.length = 1;
515 vector[0].iov_base = (void *)(state->buffer.data);
516 vector[0].iov_len = 1;
521 case HTTP_MORE_DATA_EXPECTED:
522 /* TODO Optimize, allocating byte by byte */
523 state->buffer.data = talloc_realloc(state, state->buffer.data,
524 uint8_t, state->buffer.length + 1);
525 if (!state->buffer.data) {
528 state->buffer.length++;
529 vector[0].iov_base = (void *)(state->buffer.data +
530 state->buffer.length - 1);
531 vector[0].iov_len = 1;
535 case HTTP_DATA_CORRUPTED:
536 case HTTP_REQUEST_CANCELED:
537 case HTTP_DATA_TOO_LONG:
541 DEBUG(0, ("%s: Unexpected status\n", __func__));
549 * Reads a HTTP response
551 static void http_read_response_done(struct tevent_req *);
552 struct tevent_req *http_read_response_send(TALLOC_CTX *mem_ctx,
553 struct tevent_context *ev,
554 struct tstream_context *stream,
555 size_t max_content_length)
557 struct tevent_req *req;
558 struct tevent_req *subreq;
559 struct http_read_response_state *state;
561 DEBUG(11, ("%s: Reading HTTP response\n", __func__));
564 if (!ev || !stream) {
565 DEBUG(0, ("%s: Invalid parameter\n", __func__));
569 req = tevent_req_create(mem_ctx, &state, struct http_read_response_state);
574 state->max_headers_size = HTTP_MAX_HEADER_SIZE;
575 state->max_content_length = (uint64_t)max_content_length;
576 state->parser_state = HTTP_READING_FIRSTLINE;
577 state->response = talloc_zero(state, struct http_request);
578 if (tevent_req_nomem(state->response, req)) {
579 return tevent_req_post(req, ev);
582 subreq = tstream_readv_pdu_send(state, ev, stream,
583 http_read_response_next_vector,
585 if (tevent_req_nomem(subreq,req)) {
586 return tevent_req_post(req, ev);
588 tevent_req_set_callback(subreq, http_read_response_done, req);
593 static void http_read_response_done(struct tevent_req *subreq)
596 struct tevent_req *req;
601 DEBUG(0, ("%s: Invalid parameter\n", __func__));
605 req = tevent_req_callback_data(subreq, struct tevent_req);
607 ret = tstream_readv_pdu_recv(subreq, &sys_errno);
608 DEBUG(11, ("%s: HTTP response read (%d bytes)\n", __func__, ret));
611 status = map_nt_error_from_unix_common(sys_errno);
612 DEBUG(0, ("%s: Failed to read HTTP response: %s\n",
613 __func__, nt_errstr(status)));
614 tevent_req_nterror(req, status);
618 tevent_req_done(req);
621 NTSTATUS http_read_response_recv(struct tevent_req *req,
623 struct http_request **response)
626 struct http_read_response_state *state;
628 if (!mem_ctx || !response || !req) {
629 DEBUG(0, ("%s: Invalid parameter\n", __func__));
630 return NT_STATUS_INVALID_PARAMETER;
632 if (tevent_req_is_nterror(req, &status)) {
633 tevent_req_received(req);
637 state = tevent_req_data(req, struct http_read_response_state);
638 *response = state->response;
639 talloc_steal(mem_ctx, state->response);
641 tevent_req_received(req);
646 static const char *http_method_str(enum http_cmd_type type)
651 case HTTP_REQ_RPC_IN_DATA:
652 method = "RPC_IN_DATA";
654 case HTTP_REQ_RPC_OUT_DATA:
655 method = "RPC_OUT_DATA";
665 static NTSTATUS http_push_request_line(TALLOC_CTX *mem_ctx,
667 const struct http_request *req)
673 if (!buffer || !req) {
674 DEBUG(0, ("%s: Invalid parameter\n", __func__));
675 return NT_STATUS_INVALID_PARAMETER;
678 method = http_method_str(req->type);
679 if (method == NULL) {
680 return NT_STATUS_INVALID_PARAMETER;
683 str = talloc_asprintf(mem_ctx, "%s %s HTTP/%c.%c\r\n", method,
684 req->uri, req->major, req->minor);
686 return NT_STATUS_NO_MEMORY;
688 if (!data_blob_append(mem_ctx, buffer, str, strlen(str))) {
690 return NT_STATUS_NO_MEMORY;
697 static NTSTATUS http_push_headers(TALLOC_CTX *mem_ctx,
699 struct http_request *req)
701 struct http_header *header = NULL;
702 char *header_str = NULL;
707 DEBUG(0, ("%s: Invalid parameter\n", __func__));
708 return NT_STATUS_INVALID_PARAMETER;
711 for (header = req->headers; header != NULL; header = header->next) {
712 header_str = talloc_asprintf(mem_ctx, "%s: %s\r\n",
713 header->key, header->value);
714 if (header_str == NULL) {
715 return NT_STATUS_NO_MEMORY;
718 len = strlen(header_str);
719 if (!data_blob_append(mem_ctx, blob, header_str, len)) {
720 talloc_free(header_str);
721 return NT_STATUS_NO_MEMORY;
723 talloc_free(header_str);
726 if (!data_blob_append(mem_ctx, blob, "\r\n",2)) {
727 return NT_STATUS_NO_MEMORY;
734 static NTSTATUS http_push_body(TALLOC_CTX *mem_ctx,
736 struct http_request *req)
740 DEBUG(0, ("%s: Invalid parameter\n", __func__));
741 return NT_STATUS_INVALID_PARAMETER;
744 if (req->body.length) {
745 if (!data_blob_append(mem_ctx, blob, req->body.data,
747 return NT_STATUS_NO_MEMORY;
755 * Sends and HTTP request
757 static void http_send_request_done(struct tevent_req *);
758 struct tevent_req *http_send_request_send(TALLOC_CTX *mem_ctx,
759 struct tevent_context *ev,
760 struct tstream_context *stream,
761 struct tevent_queue *send_queue,
762 struct http_request *request)
764 struct tevent_req *req;
765 struct tevent_req *subreq;
766 struct http_send_request_state *state = NULL;
769 DEBUG(11, ("%s: Sending HTTP request\n", __func__));
772 if (!ev || !stream || !send_queue || !request) {
773 DEBUG(0, ("%s: Invalid parameter\n", __func__));
777 req = tevent_req_create(mem_ctx, &state, struct http_send_request_state);
783 state->stream = stream;
784 state->send_queue = send_queue;
785 state->request = request;
787 /* Push the request line */
788 status = http_push_request_line(state, &state->buffer, state->request);
789 if (!NT_STATUS_IS_OK(status)) {
790 tevent_req_nterror(req, status);
791 return tevent_req_post(req, ev);
794 /* Push the headers */
795 status = http_push_headers(mem_ctx, &state->buffer, request);
796 if (!NT_STATUS_IS_OK(status)) {
797 tevent_req_nterror(req, status);
798 return tevent_req_post(req, ev);
802 status = http_push_body(mem_ctx, &state->buffer, request);
803 if (!NT_STATUS_IS_OK(status)) {
804 tevent_req_nterror(req, status);
805 return tevent_req_post(req, ev);
808 state->iov.iov_base = (char *) state->buffer.data;
809 state->iov.iov_len = state->buffer.length;
810 subreq = tstream_writev_queue_send(state, ev, stream, send_queue,
812 if (tevent_req_nomem(subreq, req)) {
813 return tevent_req_post(req, ev);
815 tevent_req_set_callback(subreq, http_send_request_done, req);
820 static void http_send_request_done(struct tevent_req *subreq)
823 struct tevent_req *req;
824 struct http_send_request_state *state;
826 req = tevent_req_callback_data(subreq, struct tevent_req);
827 state = tevent_req_data(req, struct http_send_request_state);
829 state->nwritten = tstream_writev_queue_recv(subreq, &state->sys_errno);
831 if (state->nwritten == -1 && state->sys_errno != 0) {
832 status = map_nt_error_from_unix_common(state->sys_errno);
833 DEBUG(0, ("%s: Failed to send HTTP request: %s\n",
834 __func__, nt_errstr(status)));
835 tevent_req_nterror(req, status);
839 tevent_req_done(req);
842 NTSTATUS http_send_request_recv(struct tevent_req *req)
847 DEBUG(0, ("%s: Invalid parameter\n", __func__));
848 return NT_STATUS_INVALID_PARAMETER;
851 if (tevent_req_is_nterror(req, &status)) {
852 tevent_req_received(req);
856 tevent_req_received(req);