- allow to avoid deps on shared extensions on build time
[m6w6/ext-http] / http_info_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #include "php_http.h"
16
17 #include "php_http_api.h"
18 #include "php_http_info_api.h"
19
20 PHP_HTTP_API void _http_info_default_callback(void **nothing, HashTable **headers, http_info *info TSRMLS_DC)
21 {
22 zval array;
23
24 INIT_ZARR(array, *headers);
25
26 switch (info->type) {
27 case IS_HTTP_REQUEST:
28 add_assoc_string(&array, "Request Method", HTTP_INFO(info).request.method, 1);
29 add_assoc_string(&array, "Request Url", HTTP_INFO(info).request.url, 1);
30 break;
31
32 case IS_HTTP_RESPONSE:
33 add_assoc_long(&array, "Response Code", (long) HTTP_INFO(info).response.code);
34 add_assoc_string(&array, "Response Status", HTTP_INFO(info).response.status, 1);
35 break;
36 }
37 }
38
39 PHP_HTTP_API void _http_info_dtor(http_info *i)
40 {
41 switch (i->type) {
42 case IS_HTTP_REQUEST:
43 STR_SET(HTTP_INFO(i).request.method, NULL);
44 STR_SET(HTTP_INFO(i).request.url, NULL);
45 break;
46
47 case IS_HTTP_RESPONSE:
48 STR_SET(HTTP_INFO(i).response.status, NULL);
49 break;
50
51 default:
52 break;
53 }
54 }
55
56 #if !defined(ZEND_ENGINE_2)
57 inline char *php_memnstr(char *h, char *n, size_t n_len, char *e)
58 {
59 char *p;
60
61 if (e > h && n_len > 0) {
62 while (h != e) {
63 if (*h == *n) {
64 for (p = n; *p == h[p-n]; ++p) {
65 if (p == n+n_len-1) {
66 return h;
67 }
68 }
69 }
70 ++h;
71 }
72 }
73
74 return NULL;
75 }
76 #endif
77
78 PHP_HTTP_API STATUS _http_info_parse_ex(const char *pre_header, http_info *info, zend_bool silent TSRMLS_DC)
79 {
80 const char *end, *http;
81
82 /* sane parameter */
83 if ((!pre_header) || (!*pre_header)) {
84 if (!silent) {
85 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Empty pre-header HTTP info");
86 }
87 return FAILURE;
88 }
89
90 /* where's the end of the line */
91 if (!(end = http_locate_eol(pre_header, NULL))) {
92 end = pre_header + strlen(pre_header);
93 }
94
95 /* there must be HTTP/1.x in the line
96 * and nothing than SPACE or NUL after HTTP/1.x
97 */
98 if ( (!(http = php_memnstr((char *) pre_header, "HTTP/1.", lenof("HTTP/1."), (char *)end))) ||
99 (!(http < end)) ||
100 (!isdigit(http[lenof("HTTP/1.")])) ||
101 (http[lenof("HTTP/1.1")] && (!isspace(http[lenof("HTTP/1.1")])))) {
102 if (!silent) {
103 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Invalid or missing HTTP/1.x protocol identification");
104 }
105 return FAILURE;
106 }
107
108 #if 0
109 {
110 char *line = estrndup(pre_header, end - pre_header);
111 fprintf(stderr, "http_parse_info('%s')\n", line);
112 efree(line);
113 }
114 #endif
115
116 info->http.version = atof(http + lenof("HTTP/"));
117
118 /* is response */
119 if (pre_header == http) {
120 char *status = NULL;
121 const char *code = http + sizeof("HTTP/1.1");
122
123 info->type = IS_HTTP_RESPONSE;
124 HTTP_INFO(info).response.code = (code && (end > code)) ? strtol(code, &status, 10) : 0;
125 HTTP_INFO(info).response.status = (status && (end > ++status)) ? estrndup(status, end - status) : ecalloc(1,1);
126
127 return SUCCESS;
128 }
129
130 /* is request */
131 else if (!http[lenof("HTTP/1.x")] || http[lenof("HTTP/1.x")] == '\r' || http[lenof("HTTP/1.x")] == '\n') {
132 const char *url = strchr(pre_header, ' ');
133
134 info->type = IS_HTTP_REQUEST;
135 if (url && http > url) {
136 HTTP_INFO(info).request.method = estrndup(pre_header, url - pre_header);
137 HTTP_INFO(info).request.url = estrndup(url + 1, http - url - 2);
138 } else {
139 HTTP_INFO(info).request.method = ecalloc(1,1);
140 HTTP_INFO(info).request.url = ecalloc(1,1);
141 }
142
143 return SUCCESS;
144 }
145
146 /* some darn header containing HTTP/1.x */
147 else {
148 return FAILURE;
149 }
150 }
151
152
153 /*
154 * Local variables:
155 * tab-width: 4
156 * c-basic-offset: 4
157 * End:
158 * vim600: noet sw=4 ts=4 fdm=marker
159 * vim<600: noet sw=4 ts=4
160 */
161