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__));
477 state = talloc_get_type_abort(private_data, struct http_read_response_state);
478 vector = talloc_array(mem_ctx, struct iovec, 1);
480 DEBUG(0, ("%s: No more memory\n", __func__));
484 if (state->buffer.data == NULL) {
485 /* Allocate buffer */
486 state->buffer.data = talloc_zero_array(state, uint8_t, 1);
487 if (!state->buffer.data) {
488 DEBUG(0, ("%s: No more memory\n", __func__));
491 state->buffer.length = 1;
493 /* Return now, nothing to parse yet */
494 vector[0].iov_base = (void *)(state->buffer.data);
495 vector[0].iov_len = 1;
501 switch (http_parse_buffer(state)) {
502 case HTTP_ALL_DATA_READ:
503 if (state->parser_state == HTTP_READING_DONE) {
504 /* Full request or response parsed */
508 /* Free current buffer and allocate new one */
509 TALLOC_FREE(state->buffer.data);
510 state->buffer.data = talloc_zero_array(state, uint8_t, 1);
511 if (!state->buffer.data) {
514 state->buffer.length = 1;
516 vector[0].iov_base = (void *)(state->buffer.data);
517 vector[0].iov_len = 1;
522 case HTTP_MORE_DATA_EXPECTED:
523 /* TODO Optimize, allocating byte by byte */
524 state->buffer.data = talloc_realloc(state, state->buffer.data,
525 uint8_t, state->buffer.length + 1);
526 if (!state->buffer.data) {
529 state->buffer.length++;
530 vector[0].iov_base = (void *)(state->buffer.data +
531 state->buffer.length - 1);
532 vector[0].iov_len = 1;
536 case HTTP_DATA_CORRUPTED:
537 case HTTP_REQUEST_CANCELED:
538 case HTTP_DATA_TOO_LONG:
542 DEBUG(0, ("%s: Unexpected status\n", __func__));
550 * Reads a HTTP response
552 static void http_read_response_done(struct tevent_req *);
553 struct tevent_req *http_read_response_send(TALLOC_CTX *mem_ctx,
554 struct tevent_context *ev,
555 struct tstream_context *stream,
556 size_t max_content_length)
558 struct tevent_req *req;
559 struct tevent_req *subreq;
560 struct http_read_response_state *state;
562 DEBUG(11, ("%s: Reading HTTP response\n", __func__));
565 if (!ev || !stream) {
566 DEBUG(0, ("%s: Invalid parameter\n", __func__));
570 req = tevent_req_create(mem_ctx, &state, struct http_read_response_state);
575 state->max_headers_size = HTTP_MAX_HEADER_SIZE;
576 state->max_content_length = (uint64_t)max_content_length;
577 state->parser_state = HTTP_READING_FIRSTLINE;
578 state->response = talloc_zero(state, struct http_request);
579 if (tevent_req_nomem(state->response, req)) {
580 return tevent_req_post(req, ev);
583 subreq = tstream_readv_pdu_send(state, ev, stream,
584 http_read_response_next_vector,
586 if (tevent_req_nomem(subreq,req)) {
587 return tevent_req_post(req, ev);
589 tevent_req_set_callback(subreq, http_read_response_done, req);
594 static void http_read_response_done(struct tevent_req *subreq)
597 struct tevent_req *req;
602 DEBUG(0, ("%s: Invalid parameter\n", __func__));
606 req = tevent_req_callback_data(subreq, struct tevent_req);
608 ret = tstream_readv_pdu_recv(subreq, &sys_errno);
609 DEBUG(11, ("%s: HTTP response read (%d bytes)\n", __func__, ret));
612 status = map_nt_error_from_unix_common(sys_errno);
613 DEBUG(0, ("%s: Failed to read HTTP response: %s\n",
614 __func__, nt_errstr(status)));
615 tevent_req_nterror(req, status);
619 tevent_req_done(req);
622 NTSTATUS http_read_response_recv(struct tevent_req *req,
624 struct http_request **response)
627 struct http_read_response_state *state;
629 if (!mem_ctx || !response || !req) {
630 DEBUG(0, ("%s: Invalid parameter\n", __func__));
631 return NT_STATUS_INVALID_PARAMETER;
633 if (tevent_req_is_nterror(req, &status)) {
634 tevent_req_received(req);
638 state = tevent_req_data(req, struct http_read_response_state);
639 *response = state->response;
640 talloc_steal(mem_ctx, state->response);
642 tevent_req_received(req);
647 static const char *http_method_str(enum http_cmd_type type)
652 case HTTP_REQ_RPC_IN_DATA:
653 method = "RPC_IN_DATA";
655 case HTTP_REQ_RPC_OUT_DATA:
656 method = "RPC_OUT_DATA";
666 static NTSTATUS http_push_request_line(TALLOC_CTX *mem_ctx,
668 const struct http_request *req)
674 if (!buffer || !req) {
675 DEBUG(0, ("%s: Invalid parameter\n", __func__));
676 return NT_STATUS_INVALID_PARAMETER;
679 method = http_method_str(req->type);
680 if (method == NULL) {
681 return NT_STATUS_INVALID_PARAMETER;
684 str = talloc_asprintf(mem_ctx, "%s %s HTTP/%c.%c\r\n", method,
685 req->uri, req->major, req->minor);
687 return NT_STATUS_NO_MEMORY;
689 if (!data_blob_append(mem_ctx, buffer, str, strlen(str))) {
691 return NT_STATUS_NO_MEMORY;
698 static NTSTATUS http_push_headers(TALLOC_CTX *mem_ctx,
700 struct http_request *req)
702 struct http_header *header = NULL;
703 char *header_str = NULL;
708 DEBUG(0, ("%s: Invalid parameter\n", __func__));
709 return NT_STATUS_INVALID_PARAMETER;
712 for (header = req->headers; header != NULL; header = header->next) {
713 header_str = talloc_asprintf(mem_ctx, "%s: %s\r\n",
714 header->key, header->value);
715 if (header_str == NULL) {
716 return NT_STATUS_NO_MEMORY;
719 len = strlen(header_str);
720 if (!data_blob_append(mem_ctx, blob, header_str, len)) {
721 talloc_free(header_str);
722 return NT_STATUS_NO_MEMORY;
724 talloc_free(header_str);
727 if (!data_blob_append(mem_ctx, blob, "\r\n",2)) {
728 return NT_STATUS_NO_MEMORY;
735 static NTSTATUS http_push_body(TALLOC_CTX *mem_ctx,
737 struct http_request *req)
741 DEBUG(0, ("%s: Invalid parameter\n", __func__));
742 return NT_STATUS_INVALID_PARAMETER;
745 if (req->body.length) {
746 if (!data_blob_append(mem_ctx, blob, req->body.data,
748 return NT_STATUS_NO_MEMORY;
756 * Sends and HTTP request
758 static void http_send_request_done(struct tevent_req *);
759 struct tevent_req *http_send_request_send(TALLOC_CTX *mem_ctx,
760 struct tevent_context *ev,
761 struct tstream_context *stream,
762 struct tevent_queue *send_queue,
763 struct http_request *request)
765 struct tevent_req *req;
766 struct tevent_req *subreq;
767 struct http_send_request_state *state = NULL;
770 DEBUG(11, ("%s: Sending HTTP request\n", __func__));
773 if (!ev || !stream || !send_queue || !request) {
774 DEBUG(0, ("%s: Invalid parameter\n", __func__));
778 req = tevent_req_create(mem_ctx, &state, struct http_send_request_state);
784 state->stream = stream;
785 state->send_queue = send_queue;
786 state->request = request;
788 /* Push the request line */
789 status = http_push_request_line(state, &state->buffer, state->request);
790 if (!NT_STATUS_IS_OK(status)) {
791 tevent_req_nterror(req, status);
792 return tevent_req_post(req, ev);
795 /* Push the headers */
796 status = http_push_headers(mem_ctx, &state->buffer, request);
797 if (!NT_STATUS_IS_OK(status)) {
798 tevent_req_nterror(req, status);
799 return tevent_req_post(req, ev);
803 status = http_push_body(mem_ctx, &state->buffer, request);
804 if (!NT_STATUS_IS_OK(status)) {
805 tevent_req_nterror(req, status);
806 return tevent_req_post(req, ev);
809 state->iov.iov_base = (char *) state->buffer.data;
810 state->iov.iov_len = state->buffer.length;
811 subreq = tstream_writev_queue_send(state, ev, stream, send_queue,
813 if (tevent_req_nomem(subreq, req)) {
814 return tevent_req_post(req, ev);
816 tevent_req_set_callback(subreq, http_send_request_done, req);
821 static void http_send_request_done(struct tevent_req *subreq)
824 struct tevent_req *req;
825 struct http_send_request_state *state;
827 req = tevent_req_callback_data(subreq, struct tevent_req);
828 state = tevent_req_data(req, struct http_send_request_state);
830 state->nwritten = tstream_writev_queue_recv(subreq, &state->sys_errno);
832 if (state->nwritten == -1 && state->sys_errno != 0) {
833 status = map_nt_error_from_unix_common(state->sys_errno);
834 DEBUG(0, ("%s: Failed to send HTTP request: %s\n",
835 __func__, nt_errstr(status)));
836 tevent_req_nterror(req, status);
840 tevent_req_done(req);
843 NTSTATUS http_send_request_recv(struct tevent_req *req)
848 DEBUG(0, ("%s: Invalid parameter\n", __func__));
849 return NT_STATUS_INVALID_PARAMETER;
852 if (tevent_req_is_nterror(req, &status)) {
853 tevent_req_received(req);
857 tevent_req_received(req);