10b49cd14fee94fd6850675005187aa546391d42
[metze/samba/wip.git] / source4 / lib / http / http.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    HTTP library
5
6    Copyright (C) 2013 Samuel Cabrero <samuelcabrero@kernevil.me>
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "lib/util/tevent_ntstatus.h"
24 #include "http.h"
25 #include "http_internal.h"
26 #include "util/tevent_werror.h"
27 #include "lib/util/dlinklist.h"
28
29
30 /**
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.
34  */
35 static int http_response_needs_body(struct http_request *req)
36 {
37         struct http_header *h = NULL;
38
39         if (!req) return -1;
40
41         for (h = req->headers; h != NULL; h = h->next) {
42                 int cmp;
43                 int n;
44                 char c;
45                 unsigned long long v;
46
47                 cmp = strcasecmp(h->key, "Content-Length");
48                 if (cmp != 0) {
49                         continue;
50                 }
51
52                 n = sscanf(h->value, "%llu%c", &v, &c);
53                 if (n != 1) {
54                         return -1;
55                 }
56
57                 req->remaining_content_length = v;
58
59                 if (v != 0) {
60                         return 1;
61                 }
62
63                 return 0;
64         }
65
66         return 0;
67 }
68
69
70 /**
71  * Parses the HTTP headers
72  */
73 static enum http_read_status http_parse_headers(struct http_read_response_state *state)
74 {
75         enum http_read_status   status = HTTP_ALL_DATA_READ;
76         char                    *ptr = NULL;
77         char                    *line = NULL;
78         char                    *key = NULL;
79         char                    *value = NULL;
80         int                     n = 0;
81         int                     ret;
82
83         /* Sanity checks */
84         if (!state || !state->response) {
85                 DEBUG(0, ("%s: Invalid Parameter\n", __func__));
86                 return HTTP_DATA_CORRUPTED;
87         }
88
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;
93         }
94
95         line = talloc_strndup(state, (char *)state->buffer.data, state->buffer.length);
96         if (!line) {
97                 DEBUG(0, ("%s: Memory error\n", __func__));
98                 return HTTP_DATA_CORRUPTED;
99         }
100
101         ptr = strstr(line, "\r\n");
102         if (ptr == NULL) {
103                 TALLOC_FREE(line);
104                 return HTTP_MORE_DATA_EXPECTED;
105         }
106
107         state->response->headers_size += state->buffer.length;
108
109         if (strncmp(line, "\r\n", 2) == 0) {
110                 DEBUG(11,("%s: All headers read\n", __func__));
111
112                 ret = http_response_needs_body(state->response);
113                 switch (ret) {
114                 case 1:
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;
118                                 break;
119                         }
120                         FALL_THROUGH;
121                 case 0:
122                         DEBUG(11, ("%s: Skipping body for code %d\n", __func__,
123                                    state->response->response_code));
124                         state->parser_state = HTTP_READING_DONE;
125                         break;
126                 case -1:
127                         DEBUG(0, ("%s_: Error in http_response_needs_body\n", __func__));
128                         TALLOC_FREE(line);
129                         return HTTP_DATA_CORRUPTED;
130                         break;
131                 }
132
133                 TALLOC_FREE(line);
134                 return HTTP_ALL_DATA_READ;
135         }
136
137         n = sscanf(line, "%m[^:]: %m[^\r\n]\r\n", &key, &value);
138         if (n != 2) {
139                 DEBUG(0, ("%s: Error parsing header '%s'\n", __func__, line));
140                 status = HTTP_DATA_CORRUPTED;
141                 goto error;
142         }
143
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;
147                 goto error;
148         }
149
150 error:
151         free(key);
152         free(value);
153         TALLOC_FREE(line);
154         return status;
155 }
156
157 /**
158  * Parses the first line of a HTTP response
159  */
160 static bool http_parse_response_line(struct http_read_response_state *state)
161 {
162         bool    status = true;
163         char    *protocol;
164         char    *msg = NULL;
165         char    major;
166         char    minor;
167         int     code;
168         char    *line = NULL;
169         int     n;
170
171         /* Sanity checks */
172         if (!state) {
173                 DEBUG(0, ("%s: Input parameter is NULL\n", __func__));
174                 return false;
175         }
176
177         line = talloc_strndup(state, (char*)state->buffer.data, state->buffer.length);
178         if (!line) {
179                 DEBUG(0, ("%s: Memory error\n", __func__));
180                 return false;
181         }
182
183         n = sscanf(line, "%m[^/]/%c.%c %d %m[^\r\n]\r\n",
184                    &protocol, &major, &minor, &code, &msg);
185
186         DEBUG(11, ("%s: Header parsed(%i): protocol->%s, major->%c, minor->%c, "
187                    "code->%d, message->%s\n", __func__, n, protocol, major, minor,
188                    code, msg));
189
190         if (n != 5) {
191                 DEBUG(0, ("%s: Error parsing header\n", __func__));
192                 status = false;
193                 goto error;
194         }
195
196         if (major != '1') {
197                 DEBUG(0, ("%s: Bad HTTP major number '%c'\n", __func__, major));
198                 status = false;
199                 goto error;
200         }
201
202         if (code == 0) {
203                 DEBUG(0, ("%s: Bad response code '%d'", __func__, code));
204                 status = false;
205                 goto error;
206         }
207
208         if (msg == NULL) {
209                 DEBUG(0, ("%s: Error parsing HTTP data\n", __func__));
210                 status = false;
211                 goto error;
212         }
213
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,
218                                                              msg, strlen(msg));
219
220 error:
221         free(protocol);
222         free(msg);
223         TALLOC_FREE(line);
224         return status;
225 }
226
227 /*
228  * Parses header lines from a request or a response into the specified
229  * request object given a buffer.
230  *
231  * Returns
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
236  */
237 static enum http_read_status http_parse_firstline(struct http_read_response_state *state)
238 {
239         enum http_read_status   status = HTTP_ALL_DATA_READ;
240         char                    *ptr = NULL;
241         char                    *line;
242
243         /* Sanity checks */
244         if (!state) {
245                 DEBUG(0, ("%s: Invalid Parameter\n", __func__));
246                 return HTTP_DATA_CORRUPTED;
247         }
248
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;
253         }
254
255         line = talloc_strndup(state, (char *)state->buffer.data, state->buffer.length);
256         if (!line) {
257                 DEBUG(0, ("%s: Not enough memory\n", __func__));
258                 return HTTP_DATA_CORRUPTED;
259         }
260
261         ptr = strstr(line, "\r\n");
262         if (ptr == NULL) {
263                 TALLOC_FREE(line);
264                 return HTTP_MORE_DATA_EXPECTED;
265         }
266
267         state->response->headers_size = state->buffer.length;
268         if (!http_parse_response_line(state)) {
269                 status = HTTP_DATA_CORRUPTED;
270         }
271
272         /* Next state, read HTTP headers */
273         state->parser_state = HTTP_READING_HEADERS;
274
275         TALLOC_FREE(line);
276         return status;
277 }
278
279 static enum http_read_status http_read_body(struct http_read_response_state *state)
280 {
281         struct http_request *resp = state->response;
282
283         if (state->buffer.length < resp->remaining_content_length) {
284                 return HTTP_MORE_DATA_EXPECTED;
285         }
286
287         resp->body = state->buffer;
288         state->buffer = data_blob_null;
289         talloc_steal(resp, resp->body.data);
290         resp->remaining_content_length = 0;
291
292         state->parser_state = HTTP_READING_DONE;
293         return HTTP_ALL_DATA_READ;
294 }
295
296 static enum http_read_status http_read_trailer(struct http_read_response_state *state)
297 {
298         enum http_read_status status = HTTP_DATA_CORRUPTED;
299         /* TODO */
300         return status;
301 }
302
303 static enum http_read_status http_parse_buffer(struct http_read_response_state *state)
304 {
305         if (!state) {
306                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
307                 return HTTP_DATA_CORRUPTED;
308         }
309
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);
317                         break;
318                 case HTTP_READING_TRAILER:
319                         return http_read_trailer(state);
320                         break;
321                 case HTTP_READING_DONE:
322                         /* All read */
323                         return HTTP_ALL_DATA_READ;
324                 default:
325                         DEBUG(0, ("%s: Illegal parser state %d", __func__,
326                                   state->parser_state));
327                         break;
328         }
329         return HTTP_DATA_CORRUPTED;
330 }
331
332 static int http_header_is_valid_value(const char *value)
333 {
334         const char      *p = NULL;
335
336         /* Sanity checks */
337         if (!value) {
338                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
339                 return -1;
340         }
341         p = value;
342
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')
348                         return (0);
349         }
350         return 1;
351 }
352
353 static int http_add_header_internal(TALLOC_CTX *mem_ctx,
354                                     struct http_header **headers,
355                                     const char *key, const char *value,
356                                     bool replace)
357 {
358         struct http_header *tail = NULL;
359         struct http_header *h = NULL;
360
361         /* Sanity checks */
362         if (!headers || !key || !value) {
363                 DEBUG(0, ("Invalid parameter\n"));
364                 return -1;
365         }
366
367
368
369         if (replace) {
370                 for (h = *headers; h != NULL; h = h->next) {
371                         if (strcasecmp(key, h->key) == 0) {
372                                 break;
373                         }
374                 }
375
376                 if (h != NULL) {
377                         /* Replace header value */
378                         if (h->value) {
379                                 talloc_free(h->value);
380                         }
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));
384                         return 0;
385                 }
386         }
387
388         /* Add new header */
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);
394         if (tail != h) {
395                 DEBUG(0, ("%s: Error adding header\n", __func__));
396                 return -1;
397         }
398         DEBUG(11, ("%s: Added HTTP header: key '%s', value '%s'\n",
399                         __func__, h->key, h->value));
400         return 0;
401 }
402
403 int http_add_header(TALLOC_CTX *mem_ctx,
404                     struct http_header **headers,
405                     const char *key, const char *value)
406 {
407         if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
408                 DEBUG(0, ("%s: Dropping illegal header key\n", __func__));
409                 return -1;
410         }
411
412         if (!http_header_is_valid_value(value)) {
413                 DEBUG(0, ("%s: Dropping illegal header value\n", __func__));
414                 return -1;
415         }
416
417         return (http_add_header_internal(mem_ctx, headers, key, value, false));
418 }
419
420 int http_replace_header(TALLOC_CTX *mem_ctx,
421                     struct http_header **headers,
422                     const char *key, const char *value)
423 {
424         if (strchr(key, '\r') != NULL || strchr(key, '\n') != NULL) {
425                 DEBUG(0, ("%s: Dropping illegal header key\n", __func__));
426                 return -1;
427         }
428
429         if (!http_header_is_valid_value(value)) {
430                 DEBUG(0, ("%s: Dropping illegal header value\n", __func__));
431                 return -1;
432         }
433
434         return (http_add_header_internal(mem_ctx, headers, key, value, true));
435 }
436
437 /**
438  * Remove a header from the headers list.
439  *
440  * Returns 0,  if the header was successfully removed.
441  * Returns -1, if the header could not be found.
442  */
443 int http_remove_header(struct http_header **headers, const char *key)
444 {
445         struct http_header *header;
446
447         /* Sanity checks */
448         if (!headers || !key) {
449                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
450                 return -1;
451         }
452
453         for(header = *headers; header != NULL; header = header->next) {
454                 if (strcmp(key, header->key) == 0) {
455                         DLIST_REMOVE(*headers, header);
456                         return 0;
457                 }
458         }
459         return -1;
460 }
461
462 static int http_read_response_next_vector(struct tstream_context *stream,
463                                           void *private_data,
464                                           TALLOC_CTX *mem_ctx,
465                                           struct iovec **_vector,
466                                           size_t *_count)
467 {
468         struct http_read_response_state *state;
469         struct iovec                    *vector;
470
471         /* Sanity checks */
472         if (!stream || !private_data || !_vector || !_count) {
473                 DEBUG(0, ("%s: Invalid Parameter\n", __func__));
474         }
475
476         state = talloc_get_type_abort(private_data, struct http_read_response_state);
477         vector = talloc_array(mem_ctx, struct iovec, 1);
478         if (!vector) {
479                 DEBUG(0, ("%s: No more memory\n", __func__));
480                 return -1;
481         }
482
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__));
488                         return -1;
489                 }
490                 state->buffer.length = 1;
491
492                 /* Return now, nothing to parse yet */
493                 vector[0].iov_base = (void *)(state->buffer.data);
494                 vector[0].iov_len = 1;
495                 *_vector = vector;
496                 *_count = 1;
497                 return 0;
498         }
499
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 */
504                                 *_vector = NULL;
505                                 *_count = 0;
506                         } else {
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) {
511                                         return -1;
512                                 }
513                                 state->buffer.length = 1;
514
515                                 vector[0].iov_base = (void *)(state->buffer.data);
516                                 vector[0].iov_len = 1;
517                                 *_vector = vector;
518                                 *_count = 1;
519                         }
520                         break;
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) {
526                                 return -1;
527                         }
528                         state->buffer.length++;
529                         vector[0].iov_base = (void *)(state->buffer.data +
530                                                       state->buffer.length - 1);
531                         vector[0].iov_len = 1;
532                         *_vector = vector;
533                         *_count = 1;
534                         break;
535                 case HTTP_DATA_CORRUPTED:
536                 case HTTP_REQUEST_CANCELED:
537                 case HTTP_DATA_TOO_LONG:
538                         return -1;
539                         break;
540                 default:
541                         DEBUG(0, ("%s: Unexpected status\n", __func__));
542                         break;
543         }
544         return 0;
545 }
546
547
548 /**
549  * Reads a HTTP response
550  */
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)
556 {
557         struct tevent_req               *req;
558         struct tevent_req               *subreq;
559         struct http_read_response_state *state;
560
561         DEBUG(11, ("%s: Reading HTTP response\n", __func__));
562
563         /* Sanity checks */
564         if (!ev || !stream) {
565                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
566                 return NULL;
567         }
568
569         req = tevent_req_create(mem_ctx, &state, struct http_read_response_state);
570         if (req == NULL) {
571                 return NULL;
572         }
573
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);
580         }
581
582         subreq = tstream_readv_pdu_send(state, ev, stream,
583                                         http_read_response_next_vector,
584                                         state);
585         if (tevent_req_nomem(subreq,req)) {
586                 return tevent_req_post(req, ev);
587         }
588         tevent_req_set_callback(subreq, http_read_response_done, req);
589
590         return req;
591 }
592
593 static void http_read_response_done(struct tevent_req *subreq)
594 {
595         NTSTATUS                        status;
596         struct tevent_req               *req;
597         int                             ret;
598         int                             sys_errno;
599
600         if (!subreq) {
601                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
602                 return;
603         }
604
605         req = tevent_req_callback_data(subreq, struct tevent_req);
606
607         ret = tstream_readv_pdu_recv(subreq, &sys_errno);
608         DEBUG(11, ("%s: HTTP response read (%d bytes)\n", __func__, ret));
609         TALLOC_FREE(subreq);
610         if (ret == -1) {
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);
615                 return;
616         }
617
618         tevent_req_done(req);
619 }
620
621 NTSTATUS http_read_response_recv(struct tevent_req *req,
622                                  TALLOC_CTX *mem_ctx,
623                                  struct http_request **response)
624 {
625         NTSTATUS status;
626         struct http_read_response_state *state;
627
628         if (!mem_ctx || !response || !req) {
629                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
630                 return NT_STATUS_INVALID_PARAMETER;
631         }
632         if (tevent_req_is_nterror(req, &status)) {
633                 tevent_req_received(req);
634                 return status;
635         }
636
637         state = tevent_req_data(req, struct http_read_response_state);
638         *response = state->response;
639         talloc_steal(mem_ctx, state->response);
640
641         tevent_req_received(req);
642
643         return NT_STATUS_OK;
644 }
645
646 static const char *http_method_str(enum http_cmd_type type)
647 {
648         const char *method;
649
650         switch (type) {
651         case HTTP_REQ_RPC_IN_DATA:
652                 method = "RPC_IN_DATA";
653                 break;
654         case HTTP_REQ_RPC_OUT_DATA:
655                 method = "RPC_OUT_DATA";
656                 break;
657         default:
658                 method = NULL;
659                 break;
660         }
661
662         return method;
663 }
664
665 static NTSTATUS http_push_request_line(TALLOC_CTX *mem_ctx,
666                                        DATA_BLOB *buffer,
667                                        const struct http_request *req)
668 {
669         const char      *method;
670         char            *str;
671
672         /* Sanity checks */
673         if (!buffer || !req) {
674                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
675                 return NT_STATUS_INVALID_PARAMETER;
676         }
677
678         method = http_method_str(req->type);
679         if (method == NULL) {
680                 return NT_STATUS_INVALID_PARAMETER;
681         }
682
683         str = talloc_asprintf(mem_ctx, "%s %s HTTP/%c.%c\r\n", method,
684                               req->uri, req->major, req->minor);
685         if (str == NULL)
686                 return NT_STATUS_NO_MEMORY;
687
688         if (!data_blob_append(mem_ctx, buffer, str, strlen(str))) {
689                 talloc_free(str);
690                 return NT_STATUS_NO_MEMORY;
691         }
692
693         talloc_free(str);
694         return NT_STATUS_OK;
695 }
696
697 static NTSTATUS http_push_headers(TALLOC_CTX *mem_ctx,
698                                   DATA_BLOB *blob,
699                                   struct http_request *req)
700 {
701         struct http_header      *header = NULL;
702         char                    *header_str = NULL;
703         size_t                  len;
704
705         /* Sanity checks */
706         if (!blob || !req) {
707                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
708                 return NT_STATUS_INVALID_PARAMETER;
709         }
710
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;
716                 }
717
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;
722                 }
723                 talloc_free(header_str);
724         }
725
726         if (!data_blob_append(mem_ctx, blob, "\r\n",2)) {
727                 return NT_STATUS_NO_MEMORY;
728         }
729
730         return NT_STATUS_OK;
731 }
732
733
734 static NTSTATUS http_push_body(TALLOC_CTX *mem_ctx,
735                                DATA_BLOB *blob,
736                                struct http_request *req)
737 {
738         /* Sanity checks */
739         if (!blob || !req) {
740                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
741                 return NT_STATUS_INVALID_PARAMETER;
742         }
743
744         if (req->body.length) {
745                 if (!data_blob_append(mem_ctx, blob, req->body.data,
746                                 req->body.length)) {
747                         return NT_STATUS_NO_MEMORY;
748                 }
749         }
750
751         return NT_STATUS_OK;
752 }
753
754 /**
755  * Sends and HTTP request
756  */
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)
763 {
764         struct tevent_req               *req;
765         struct tevent_req               *subreq;
766         struct http_send_request_state  *state = NULL;
767         NTSTATUS                        status;
768
769         DEBUG(11, ("%s: Sending HTTP request\n", __func__));
770
771         /* Sanity checks */
772         if (!ev || !stream || !send_queue || !request) {
773                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
774                 return NULL;
775         }
776
777         req = tevent_req_create(mem_ctx, &state, struct http_send_request_state);
778         if (req == NULL) {
779                 return NULL;
780         }
781
782         state->ev = ev;
783         state->stream = stream;
784         state->send_queue = send_queue;
785         state->request = request;
786
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);
792         }
793
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);
799         }
800
801         /* Push the body */
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);
806         }
807
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,
811                                            &state->iov, 1);
812         if (tevent_req_nomem(subreq, req)) {
813                 return tevent_req_post(req, ev);
814         }
815         tevent_req_set_callback(subreq, http_send_request_done, req);
816
817         return req;
818 }
819
820 static void http_send_request_done(struct tevent_req *subreq)
821 {
822         NTSTATUS                        status;
823         struct tevent_req               *req;
824         struct http_send_request_state  *state;
825
826         req = tevent_req_callback_data(subreq, struct tevent_req);
827         state = tevent_req_data(req, struct http_send_request_state);
828
829         state->nwritten = tstream_writev_queue_recv(subreq, &state->sys_errno);
830         TALLOC_FREE(subreq);
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);
836                 return;
837         }
838
839         tevent_req_done(req);
840 }
841
842 NTSTATUS http_send_request_recv(struct tevent_req *req)
843 {
844         NTSTATUS status;
845
846         if (!req) {
847                 DEBUG(0, ("%s: Invalid parameter\n", __func__));
848                 return NT_STATUS_INVALID_PARAMETER;
849         }
850
851         if (tevent_req_is_nterror(req, &status)) {
852                 tevent_req_received(req);
853                 return status;
854         }
855
856         tevent_req_received(req);
857
858         return NT_STATUS_OK;
859 }