2 +--------------------------------------------------------------------+
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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 /* $Id: http_info_api.c 292841 2009-12-31 08:48:57Z mike $ */
17 PHP_HTTP_API
void php_http_info_default_callback(void **nothing
, HashTable
**headers
, php_http_info_t
*info TSRMLS_DC
)
22 INIT_PZVAL_ARRAY(&array
, *headers
);
25 case PHP_HTTP_REQUEST
:
26 add_assoc_string(&array
, "Request Method", PHP_HTTP_INFO(info
).request
.method
, 1);
27 add_assoc_string(&array
, "Request Url", PHP_HTTP_INFO(info
).request
.url
, 1);
30 case PHP_HTTP_RESPONSE
:
31 add_assoc_long(&array
, "Response Code", (long) PHP_HTTP_INFO(info
).response
.code
);
32 add_assoc_string(&array
, "Response Status", PHP_HTTP_INFO(info
).response
.status
, 1);
40 PHP_HTTP_API php_http_info_t
*php_http_info_init(php_http_info_t
*i TSRMLS_DC
)
43 i
= emalloc(sizeof(*i
));
46 memset(i
, 0, sizeof(*i
));
51 PHP_HTTP_API
void php_http_info_dtor(php_http_info_t
*i
)
54 case PHP_HTTP_REQUEST
:
55 STR_SET(PHP_HTTP_INFO(i
).request
.method
, NULL
);
56 STR_SET(PHP_HTTP_INFO(i
).request
.url
, NULL
);
59 case PHP_HTTP_RESPONSE
:
60 STR_SET(PHP_HTTP_INFO(i
).response
.status
, NULL
);
68 PHP_HTTP_API
void php_http_info_free(php_http_info_t
**i
)
71 php_http_info_dtor(*i
);
77 PHP_HTTP_API php_http_info_t
*php_http_info_parse(php_http_info_t
*info
, const char *pre_header TSRMLS_DC
)
79 const char *end
, *http
;
80 zend_bool free_info
= !info
;
83 if ((!pre_header
) || (!*pre_header
)) {
87 /* where's the end of the line */
88 if (!(end
= php_http_locate_eol(pre_header
, NULL
))) {
89 end
= pre_header
+ strlen(pre_header
);
92 /* there must be HTTP/1.x in the line */
93 if (!(http
= php_http_locate_str(pre_header
, end
- pre_header
, "HTTP/1.", lenof("HTTP/1.")))) {
97 info
= php_http_info_init(info TSRMLS_CC
);
99 /* and nothing than SPACE or NUL after HTTP/1.x */
100 if (!php_http_version_parse(&info
->http
.version
, http TSRMLS_CC
)
101 || (http
[lenof("HTTP/1.1")] && (!PHP_HTTP_IS_CTYPE(space
, http
[lenof("HTTP/1.1")])))) {
103 php_http_info_free(&info
);
110 char *line
= estrndup(pre_header
, end
- pre_header
);
111 fprintf(stderr
, "http_parse_info('%s')\n", line
);
117 if (pre_header
== http
) {
119 const char *code
= http
+ sizeof("HTTP/1.1");
121 info
->type
= PHP_HTTP_RESPONSE
;
122 while (' ' == *code
) ++code
;
123 if (code
&& end
> code
) {
124 PHP_HTTP_INFO(info
).response
.code
= strtol(code
, &status
, 10);
126 PHP_HTTP_INFO(info
).response
.code
= 0;
128 if (status
&& end
> status
) {
129 while (' ' == *status
) ++status
;
130 PHP_HTTP_INFO(info
).response
.status
= estrndup(status
, end
- status
);
132 PHP_HTTP_INFO(info
).response
.status
= NULL
;
139 else if (!http
[lenof("HTTP/1.x")] || http
[lenof("HTTP/1.x")] == '\r' || http
[lenof("HTTP/1.x")] == '\n') {
140 const char *url
= strchr(pre_header
, ' ');
142 info
->type
= PHP_HTTP_REQUEST
;
143 if (url
&& http
> url
) {
144 PHP_HTTP_INFO(info
).request
.method
= estrndup(pre_header
, url
- pre_header
);
145 while (' ' == *url
) ++url
;
146 while (' ' == *(http
-1)) --http
;
148 PHP_HTTP_INFO(info
).request
.url
= estrndup(url
, http
- url
);
150 efree(PHP_HTTP_INFO(info
).request
.method
);
154 PHP_HTTP_INFO(info
).request
.method
= NULL
;
155 PHP_HTTP_INFO(info
).request
.url
= NULL
;
161 /* some darn header containing HTTP/1.x */
164 php_http_info_free(&info
);
175 * vim600: noet sw=4 ts=4 fdm=marker
176 * vim<600: noet sw=4 ts=4