[f9a2831] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Martin Sucha
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup http
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[fab2746] | 36 | #include <errno.h>
|
---|
[f9a2831] | 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 | #include <macros.h>
|
---|
| 41 |
|
---|
[fab2746] | 42 | #include <inet/tcp.h>
|
---|
[f9a2831] | 43 |
|
---|
[b623b68] | 44 | #include <http/http.h>
|
---|
[f9a2831] | 45 |
|
---|
| 46 | #define HTTP_METHOD_LINE "%s %s HTTP/1.1\r\n"
|
---|
| 47 | #define HTTP_REQUEST_LINE "\r\n"
|
---|
| 48 |
|
---|
| 49 | http_request_t *http_request_create(const char *method, const char *path)
|
---|
| 50 | {
|
---|
| 51 | http_request_t *req = malloc(sizeof(http_request_t));
|
---|
| 52 | if (req == NULL)
|
---|
| 53 | return NULL;
|
---|
[a35b458] | 54 |
|
---|
[f9a2831] | 55 | req->method = str_dup(method);
|
---|
| 56 | if (req->method == NULL) {
|
---|
| 57 | free(req);
|
---|
| 58 | return NULL;
|
---|
| 59 | }
|
---|
[a35b458] | 60 |
|
---|
[f9a2831] | 61 | req->path = str_dup(path);
|
---|
| 62 | if (req->path == NULL) {
|
---|
| 63 | free(req->method);
|
---|
| 64 | free(req);
|
---|
| 65 | return NULL;
|
---|
| 66 | }
|
---|
[a35b458] | 67 |
|
---|
[4d4f656] | 68 | http_headers_init(&req->headers);
|
---|
[a35b458] | 69 |
|
---|
[f9a2831] | 70 | return req;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void http_request_destroy(http_request_t *req)
|
---|
| 74 | {
|
---|
| 75 | free(req->method);
|
---|
| 76 | free(req->path);
|
---|
[4d4f656] | 77 | http_headers_clear(&req->headers);
|
---|
[f9a2831] | 78 | free(req);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | static ssize_t http_encode_method(char *buf, size_t buf_size,
|
---|
| 82 | const char *method, const char *path)
|
---|
| 83 | {
|
---|
[4f4018b] | 84 | return snprintf(buf, buf_size, HTTP_METHOD_LINE, method, path);
|
---|
[f9a2831] | 85 | }
|
---|
| 86 |
|
---|
[b7fd2a0] | 87 | errno_t http_request_format(http_request_t *req, char **out_buf,
|
---|
[f9a2831] | 88 | size_t *out_buf_size)
|
---|
| 89 | {
|
---|
| 90 | /* Compute the size of the request */
|
---|
| 91 | ssize_t meth_size = http_encode_method(NULL, 0, req->method, req->path);
|
---|
| 92 | if (meth_size < 0)
|
---|
[1569a9b] | 93 | return EINVAL;
|
---|
[f9a2831] | 94 | size_t size = meth_size;
|
---|
[a35b458] | 95 |
|
---|
[4d4f656] | 96 | http_headers_foreach(req->headers, header) {
|
---|
[0005b63] | 97 | ssize_t header_size = http_header_encode(header, NULL, 0);
|
---|
[f9a2831] | 98 | if (header_size < 0)
|
---|
[1569a9b] | 99 | return EINVAL;
|
---|
[f9a2831] | 100 | size += header_size;
|
---|
| 101 | }
|
---|
| 102 | size += str_length(HTTP_REQUEST_LINE);
|
---|
[a35b458] | 103 |
|
---|
[f9a2831] | 104 | char *buf = malloc(size);
|
---|
| 105 | if (buf == NULL)
|
---|
| 106 | return ENOMEM;
|
---|
[a35b458] | 107 |
|
---|
[f9a2831] | 108 | char *pos = buf;
|
---|
| 109 | size_t pos_size = size;
|
---|
| 110 | ssize_t written = http_encode_method(pos, pos_size, req->method, req->path);
|
---|
| 111 | if (written < 0) {
|
---|
| 112 | free(buf);
|
---|
[1569a9b] | 113 | return EINVAL;
|
---|
[f9a2831] | 114 | }
|
---|
| 115 | pos += written;
|
---|
| 116 | pos_size -= written;
|
---|
[a35b458] | 117 |
|
---|
[4d4f656] | 118 | http_headers_foreach(req->headers, header) {
|
---|
[0005b63] | 119 | written = http_header_encode(header, pos, pos_size);
|
---|
[f9a2831] | 120 | if (written < 0) {
|
---|
| 121 | free(buf);
|
---|
[1569a9b] | 122 | return EINVAL;
|
---|
[f9a2831] | 123 | }
|
---|
| 124 | pos += written;
|
---|
| 125 | pos_size -= written;
|
---|
| 126 | }
|
---|
[a35b458] | 127 |
|
---|
[f9a2831] | 128 | size_t rlsize = str_size(HTTP_REQUEST_LINE);
|
---|
| 129 | memcpy(pos, HTTP_REQUEST_LINE, rlsize);
|
---|
| 130 | pos_size -= rlsize;
|
---|
| 131 | assert(pos_size == 0);
|
---|
[a35b458] | 132 |
|
---|
[f9a2831] | 133 | *out_buf = buf;
|
---|
| 134 | *out_buf_size = size;
|
---|
| 135 | return EOK;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[b7fd2a0] | 138 | errno_t http_send_request(http_t *http, http_request_t *req)
|
---|
[f9a2831] | 139 | {
|
---|
[db8626d] | 140 | char *buf = NULL;
|
---|
| 141 | size_t buf_size = 0;
|
---|
[a35b458] | 142 |
|
---|
[b7fd2a0] | 143 | errno_t rc = http_request_format(req, &buf, &buf_size);
|
---|
[f9a2831] | 144 | if (rc != EOK)
|
---|
| 145 | return rc;
|
---|
[a35b458] | 146 |
|
---|
[fab2746] | 147 | rc = tcp_conn_send(http->conn, buf, buf_size);
|
---|
[f9a2831] | 148 | free(buf);
|
---|
[a35b458] | 149 |
|
---|
[f9a2831] | 150 | return rc;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /** @}
|
---|
| 154 | */
|
---|