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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 PHP_HTTP_API
void php_http_info_default_callback(void **nothing
, HashTable
**headers
, php_http_info_t
*info TSRMLS_DC
)
20 INIT_PZVAL_ARRAY(&array
, *headers
);
23 case PHP_HTTP_REQUEST
:
24 add_assoc_string(&array
, "Request Method", PHP_HTTP_INFO(info
).request
.method
, 1);
25 add_assoc_string(&array
, "Request Url", PHP_HTTP_INFO(info
).request
.url
, 1);
28 case PHP_HTTP_RESPONSE
:
29 add_assoc_long(&array
, "Response Code", (long) PHP_HTTP_INFO(info
).response
.code
);
30 add_assoc_string(&array
, "Response Status", PHP_HTTP_INFO(info
).response
.status
, 1);
38 PHP_HTTP_API php_http_info_t
*php_http_info_init(php_http_info_t
*i TSRMLS_DC
)
41 i
= emalloc(sizeof(*i
));
44 memset(i
, 0, sizeof(*i
));
49 PHP_HTTP_API
void php_http_info_dtor(php_http_info_t
*i
)
52 case PHP_HTTP_REQUEST
:
53 STR_SET(PHP_HTTP_INFO(i
).request
.method
, NULL
);
54 STR_SET(PHP_HTTP_INFO(i
).request
.url
, NULL
);
57 case PHP_HTTP_RESPONSE
:
58 STR_SET(PHP_HTTP_INFO(i
).response
.status
, NULL
);
66 PHP_HTTP_API
void php_http_info_free(php_http_info_t
**i
)
69 php_http_info_dtor(*i
);
75 PHP_HTTP_API php_http_info_t
*php_http_info_parse(php_http_info_t
*info
, const char *pre_header TSRMLS_DC
)
77 const char *end
, *http
;
78 zend_bool free_info
= !info
;
81 if ((!pre_header
) || (!*pre_header
)) {
85 /* where's the end of the line */
86 if (!(end
= php_http_locate_eol(pre_header
, NULL
))) {
87 end
= pre_header
+ strlen(pre_header
);
90 /* there must be HTTP/1.x in the line */
91 if (!(http
= php_http_locate_str(pre_header
, end
- pre_header
, "HTTP/1.", lenof("HTTP/1.")))) {
95 info
= php_http_info_init(info TSRMLS_CC
);
97 /* and nothing than SPACE or NUL after HTTP/1.x */
98 if (!php_http_version_parse(&info
->http
.version
, http TSRMLS_CC
)
99 || (http
[lenof("HTTP/1.1")] && (!PHP_HTTP_IS_CTYPE(space
, http
[lenof("HTTP/1.1")])))) {
101 php_http_info_free(&info
);
108 char *line
= estrndup(pre_header
, end
- pre_header
);
109 fprintf(stderr
, "http_parse_info('%s')\n", line
);
115 if (pre_header
== http
) {
117 const char *code
= http
+ sizeof("HTTP/1.1");
119 info
->type
= PHP_HTTP_RESPONSE
;
120 while (' ' == *code
) ++code
;
121 if (code
&& end
> code
) {
122 PHP_HTTP_INFO(info
).response
.code
= strtol(code
, &status
, 10);
124 PHP_HTTP_INFO(info
).response
.code
= 0;
126 if (status
&& end
> status
) {
127 while (' ' == *status
) ++status
;
128 PHP_HTTP_INFO(info
).response
.status
= estrndup(status
, end
- status
);
130 PHP_HTTP_INFO(info
).response
.status
= NULL
;
137 else if (!http
[lenof("HTTP/1.x")] || http
[lenof("HTTP/1.x")] == '\r' || http
[lenof("HTTP/1.x")] == '\n') {
138 const char *url
= strchr(pre_header
, ' ');
140 info
->type
= PHP_HTTP_REQUEST
;
141 if (url
&& http
> url
) {
142 PHP_HTTP_INFO(info
).request
.method
= estrndup(pre_header
, url
- pre_header
);
143 while (' ' == *url
) ++url
;
144 while (' ' == *(http
-1)) --http
;
146 PHP_HTTP_INFO(info
).request
.url
= estrndup(url
, http
- url
);
148 efree(PHP_HTTP_INFO(info
).request
.method
);
152 PHP_HTTP_INFO(info
).request
.method
= NULL
;
153 PHP_HTTP_INFO(info
).request
.url
= NULL
;
159 /* some darn header containing HTTP/1.x */
162 php_http_info_free(&info
);
173 * vim600: noet sw=4 ts=4 fdm=marker
174 * vim<600: noet sw=4 ts=4