- there's no php_memnstr in PHP-4
[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 {
28 case IS_HTTP_REQUEST:
29 add_assoc_string(&array, "Request Method", HTTP_INFO(info).request.method, 1);
30 add_assoc_string(&array, "Request Url", HTTP_INFO(info).request.url, 1);
31 break;
32
33 case IS_HTTP_RESPONSE:
34 add_assoc_long(&array, "Response Code", (long) HTTP_INFO(info).response.code);
35 add_assoc_string(&array, "Response Status", HTTP_INFO(info).response.status, 1);
36 break;
37 }
38 }
39
40 PHP_HTTP_API void _http_info_dtor(http_info *i)
41 {
42 switch (i->type)
43 {
44 case IS_HTTP_REQUEST:
45 STR_SET(HTTP_INFO(i).request.method, NULL);
46 STR_SET(HTTP_INFO(i).request.url, NULL);
47 break;
48
49 case IS_HTTP_RESPONSE:
50 STR_SET(HTTP_INFO(i).response.status, NULL);
51 break;
52
53 default:
54 break;
55 }
56 }
57
58 #if !defined(ZEND_ENGINE_2)
59 inline char *php_memnstr(char *h, char *n, size_t n_len, char *e)
60 {
61 char *p;
62
63 if (e > h && b_len > 0) {
64 while (h != e) {
65 if (*h == *n) {
66 for (p = n; *p == h[p-n]; ++p) {
67 if (p == n+n_len-1) {
68 return h;
69 }
70 }
71 }
72 ++h;
73 }
74 }
75
76 return NULL;
77 }
78 #endif
79
80 PHP_HTTP_API STATUS _http_info_parse_ex(const char *pre_header, http_info *info, zend_bool silent TSRMLS_DC)
81 {
82 const char *end, *http;
83
84 /* sane parameter */
85 if ((!pre_header) || (!*pre_header)) {
86 if (!silent) {
87 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Empty pre-header HTTP info");
88 }
89 return FAILURE;
90 }
91
92 /* where's the end of the line */
93 if (!(end = http_locate_eol(pre_header, NULL))) {
94 end = pre_header + strlen(pre_header);
95 }
96
97 /* there must be HTTP/1.x in the line
98 * and nothing than SPACE or NUL after HTTP/1.x
99 */
100 if ( (!(http = php_memnstr((char *) pre_header, "HTTP/1.", lenof("HTTP/1."), (char *)end))) ||
101 (!(http < end)) ||
102 (!isdigit(http[lenof("HTTP/1.")])) ||
103 (http[lenof("HTTP/1.1")] && (!isspace(http[lenof("HTTP/1.1")])))) {
104 if (!silent) {
105 http_error(HE_WARNING, HTTP_E_MALFORMED_HEADERS, "Invalid or missing HTTP/1.x protocol identification");
106 }
107 return FAILURE;
108 }
109
110 #if 0
111 {
112 char *line = estrndup(pre_header, end - pre_header);
113 fprintf(stderr, "http_parse_info('%s')\n", line);
114 efree(line);
115 }
116 #endif
117
118 info->http.version = atof(http + lenof("HTTP/"));
119
120 /* is response */
121 if (pre_header == http) {
122 char *status = NULL;
123 const char *code = http + sizeof("HTTP/1.1");
124
125 info->type = IS_HTTP_RESPONSE;
126 HTTP_INFO(info).response.code = (code && (end > code)) ? strtol(code, &status, 10) : 0;
127 HTTP_INFO(info).response.status = (status && (end > ++status)) ? estrndup(status, end - status) : ecalloc(1,1);
128
129 return SUCCESS;
130 }
131
132 /* is request */
133 else if (!http[lenof("HTTP/1.x")] || http[lenof("HTTP/1.x")] == '\r' || http[lenof("HTTP/1.x")] == '\n') {
134 const char *url = strchr(pre_header, ' ');
135
136 info->type = IS_HTTP_REQUEST;
137 if (url && http > url) {
138 HTTP_INFO(info).request.method = estrndup(pre_header, url - pre_header);
139 HTTP_INFO(info).request.url = estrndup(url + 1, http - url - 2);
140 } else {
141 HTTP_INFO(info).request.method = ecalloc(1,1);
142 HTTP_INFO(info).request.url = ecalloc(1,1);
143 }
144
145 return SUCCESS;
146 }
147
148 /* some darn header containing HTTP/1.x */
149 else {
150 return FAILURE;
151 }
152 }
153
154
155 /*
156 * Local variables:
157 * tab-width: 4
158 * c-basic-offset: 4
159 * End:
160 * vim600: noet sw=4 ts=4 fdm=marker
161 * vim<600: noet sw=4 ts=4
162 */
163