a4490d8c4535b5a16d31535ebbd45b71bc7a05ed
[m6w6/ext-http] / http_message_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "php_http.h"
24 #include "php_http_std_defs.h"
25 #include "php_http_message_api.h"
26 #include "php_http_headers_api.h"
27
28 #include "phpstr/phpstr.h"
29
30 PHP_HTTP_API http_message *_http_message_init_ex(http_message *message, http_message_type type)
31 {
32 if (!message) {
33 message = ecalloc(1, sizeof(http_message));
34 }
35
36 message->type = type;
37 message->nested = NULL;
38 phpstr_init(&message->body);
39 zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
40
41 return message;
42 }
43
44 PHP_HTTP_API http_message *_http_message_parse_ex(char *message, size_t message_length, zend_bool dup TSRMLS_DC)
45 {
46 char *body = NULL;
47 size_t header_length = 0;
48 http_message *msg;
49
50 if (message_length < HTTP_MSG_MIN_SIZE) {
51 return NULL;
52 }
53
54 if (!message) {
55 return NULL;
56 }
57
58 msg = http_message_new();
59 msg->len = message_length;
60 msg->raw = dup ? estrndup(message, message_length) : message;
61
62 if (body = strstr(message, HTTP_CRLF HTTP_CRLF)) {
63 body += lenof(HTTP_CRLF HTTP_CRLF);
64 header_length = body - message;
65 } else {
66 header_length = message_length;
67 }
68
69 if (SUCCESS != http_parse_headers_cb(message, header_length, &msg->hdrs, 1, http_message_parse_headers_callback, (void **) &msg)) {
70 http_message_free(msg);
71 return NULL;
72 }
73
74 if (body) {
75 phpstr_from_string_ex(PHPSTR(msg), body, message_length - header_length);
76 }
77
78 return msg;
79 }
80
81 PHP_HTTP_API void _http_message_parse_headers_callback(void **message, char *http_line, size_t line_length, HashTable **headers TSRMLS_DC)
82 {
83 http_message *old = (http_message *) *message;
84 http_message *new;
85
86 if (old->type || zend_hash_num_elements(&old->hdrs) || PHPSTR_LEN(old)) {
87 new = http_message_new();
88
89 new->nested = old;
90 *message = new;
91 *headers = &new->hdrs;
92 } else {
93 new = old;
94 }
95
96 // response
97 if (!strncmp(http_line, "HTTP/1.", lenof("HTTP/1."))) {
98 new->type = HTTP_MSG_RESPONSE;
99 new->info.response.http_version = atof(http_line + lenof("HTTP/"));
100 new->info.response.status = atoi(http_line + lenof("HTTP/1.1 "));
101 } else
102 // request
103 if (!strncmp(http_line + line_length - lenof("HTTP/1.1"), "HTTP/1.", lenof("HTTP/1."))) {
104 const char *method_sep_uri = strchr(http_line, ' ');
105
106 new->type = HTTP_MSG_REQUEST;
107 new->info.request.http_version = atof(http_line + line_length - lenof("1.1"));
108 new->info.request.method = estrndup(http_line, method_sep_uri - http_line);
109 new->info.request.URI = estrndup(method_sep_uri + 1, http_line + line_length - method_sep_uri - 1 - lenof(" HTTP/1.1"));
110 }
111 }
112
113 PHP_HTTP_API void _http_message_tostring(http_message *msg, char **string, size_t *length)
114 {
115 phpstr str;
116 char *key, *data;
117 ulong idx;
118 zval **header;
119
120 phpstr_init_ex(&str, msg->len, 1);
121 /* set sane alloc size */
122 str.size = 4096;
123
124 switch (msg->type)
125 {
126 case HTTP_MSG_REQUEST:
127 phpstr_appendf(&str, "%s %s HTTP/%1.1f" HTTP_CRLF,
128 msg->info.request.method,
129 msg->info.request.URI,
130 msg->info.request.http_version);
131 break;
132
133 case HTTP_MSG_RESPONSE:
134 phpstr_appendf(&str, "HTTP/%1.1f %d" HTTP_CRLF,
135 msg->info.response.http_version,
136 msg->info.response.status);
137 break;
138 }
139
140 FOREACH_HASH_KEYVAL(&msg->hdrs, key, idx, header) {
141 if (key) {
142 zval **single_header;
143
144 switch (Z_TYPE_PP(header))
145 {
146 case IS_STRING:
147 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(header));
148 break;
149
150 case IS_ARRAY:
151 FOREACH_VAL(*header, single_header) {
152 phpstr_appendf(&str, "%s: %s" HTTP_CRLF, key, Z_STRVAL_PP(single_header));
153 }
154 break;
155 }
156
157 key = NULL;
158 }
159 }
160
161 phpstr_appends(&str, HTTP_CRLF);
162 phpstr_append(&str, PHPSTR_VAL(msg), PHPSTR_LEN(msg));
163
164 data = phpstr_data(&str, string, length);
165 if (!string) {
166 efree(data);
167 }
168
169 phpstr_dtor(&str);
170 }
171
172 PHP_HTTP_API void _http_message_dtor(http_message *message)
173 {
174 if (message) {
175 zend_hash_destroy(&message->hdrs);
176 phpstr_dtor(PHPSTR(message));
177 if (message->raw) {
178 efree(message->raw);
179 }
180 if (message->type == HTTP_MSG_REQUEST) {
181 if (message->info.request.method) {
182 efree(message->info.request.method);
183 }
184 if (message->info.request.URI) {
185 efree(message->info.request.URI);
186 }
187 }
188 }
189 }
190
191 PHP_HTTP_API void _http_message_free(http_message *message)
192 {
193 if (message) {
194 if (message->nested) {
195 http_message_free(message->nested);
196 }
197 http_message_dtor(message);
198 efree(message);
199 }
200 }
201
202 /*
203 * Local variables:
204 * tab-width: 4
205 * c-basic-offset: 4
206 * End:
207 * vim600: noet sw=4 ts=4 fdm=marker
208 * vim<600: noet sw=4 ts=4
209 */
210