source: mainline/uspace/lib/http/request.c@ f9a2831

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f9a2831 was f9a2831, checked in by Martin Sucha <sucha14@…>, 12 years ago

Split the HTTP library into several files

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[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
36#include <stdio.h>
37#include <stdlib.h>
38#include <str.h>
39#include <macros.h>
40
41#include <net/socket.h>
42
43#include "http.h"
44
45#define HTTP_METHOD_LINE "%s %s HTTP/1.1\r\n"
46#define HTTP_REQUEST_LINE "\r\n"
47
48http_request_t *http_request_create(const char *method, const char *path)
49{
50 http_request_t *req = malloc(sizeof(http_request_t));
51 if (req == NULL)
52 return NULL;
53
54 req->method = str_dup(method);
55 if (req->method == NULL) {
56 free(req);
57 return NULL;
58 }
59
60 req->path = str_dup(path);
61 if (req->path == NULL) {
62 free(req->method);
63 free(req);
64 return NULL;
65 }
66
67 list_initialize(&req->headers);
68
69 return req;
70}
71
72void http_request_destroy(http_request_t *req)
73{
74 free(req->method);
75 free(req->path);
76 link_t *link = req->headers.head.next;
77 while (link != &req->headers.head) {
78 link_t *next = link->next;
79 http_header_t *header = list_get_instance(link, http_header_t, link);
80 http_header_destroy(header);
81 link = next;
82 }
83 free(req);
84}
85
86static ssize_t http_encode_method(char *buf, size_t buf_size,
87 const char *method, const char *path)
88{
89 if (buf == NULL) {
90 return printf_size(HTTP_METHOD_LINE, method, path);
91 }
92 else {
93 return snprintf(buf, buf_size, HTTP_METHOD_LINE, method, path);
94 }
95}
96
97int http_request_format(http_request_t *req, char **out_buf,
98 size_t *out_buf_size)
99{
100 /* Compute the size of the request */
101 ssize_t meth_size = http_encode_method(NULL, 0, req->method, req->path);
102 if (meth_size < 0)
103 return meth_size;
104 size_t size = meth_size;
105
106 list_foreach(req->headers, link, http_header_t, header) {
107 ssize_t header_size = http_encode_header(NULL, 0, header);
108 if (header_size < 0)
109 return header_size;
110 size += header_size;
111 }
112 size += str_length(HTTP_REQUEST_LINE);
113
114 char *buf = malloc(size);
115 if (buf == NULL)
116 return ENOMEM;
117
118 char *pos = buf;
119 size_t pos_size = size;
120 ssize_t written = http_encode_method(pos, pos_size, req->method, req->path);
121 if (written < 0) {
122 free(buf);
123 return written;
124 }
125 pos += written;
126 pos_size -= written;
127
128 list_foreach(req->headers, link, http_header_t, header) {
129 written = http_encode_header(pos, pos_size, header);
130 if (written < 0) {
131 free(buf);
132 return written;
133 }
134 pos += written;
135 pos_size -= written;
136 }
137
138 size_t rlsize = str_size(HTTP_REQUEST_LINE);
139 memcpy(pos, HTTP_REQUEST_LINE, rlsize);
140 pos_size -= rlsize;
141 assert(pos_size == 0);
142
143 *out_buf = buf;
144 *out_buf_size = size;
145 return EOK;
146}
147
148int http_send_request(http_t *http, http_request_t *req)
149{
150 char *buf;
151 size_t buf_size;
152
153 int rc = http_request_format(req, &buf, &buf_size);
154 if (rc != EOK)
155 return rc;
156
157 rc = send(http->conn_sd, buf, buf_size, 0);
158 free(buf);
159
160 return rc;
161}
162
163/** @}
164 */
Note: See TracBrowser for help on using the repository browser.