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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 PHP_HTTP_API php_http_info_t
*php_http_info_init(php_http_info_t
*i TSRMLS_DC
)
18 i
= emalloc(sizeof(*i
));
21 memset(i
, 0, sizeof(*i
));
26 PHP_HTTP_API
void php_http_info_dtor(php_http_info_t
*i
)
29 case PHP_HTTP_REQUEST
:
30 STR_SET(PHP_HTTP_INFO(i
).request
.method
, NULL
);
31 STR_SET(PHP_HTTP_INFO(i
).request
.url
, NULL
);
34 case PHP_HTTP_RESPONSE
:
35 STR_SET(PHP_HTTP_INFO(i
).response
.status
, NULL
);
43 PHP_HTTP_API
void php_http_info_free(php_http_info_t
**i
)
46 php_http_info_dtor(*i
);
52 PHP_HTTP_API php_http_info_t
*php_http_info_parse(php_http_info_t
*info
, const char *pre_header TSRMLS_DC
)
54 const char *end
, *http
;
55 zend_bool free_info
= !info
;
58 if ((!pre_header
) || (!*pre_header
)) {
62 /* where's the end of the line */
63 if (!(end
= php_http_locate_eol(pre_header
, NULL
))) {
64 end
= pre_header
+ strlen(pre_header
);
67 /* there must be HTTP/1.x in the line */
68 if (!(http
= php_http_locate_str(pre_header
, end
- pre_header
, "HTTP/1.", lenof("HTTP/1.")))) {
72 info
= php_http_info_init(info TSRMLS_CC
);
74 /* and nothing than SPACE or NUL after HTTP/1.x */
75 if (!php_http_version_parse(&info
->http
.version
, http TSRMLS_CC
)
76 || (http
[lenof("HTTP/1.1")] && (!PHP_HTTP_IS_CTYPE(space
, http
[lenof("HTTP/1.1")])))) {
78 php_http_info_free(&info
);
85 char *line
= estrndup(pre_header
, end
- pre_header
);
86 fprintf(stderr
, "http_parse_info('%s')\n", line
);
92 if (pre_header
== http
) {
94 const char *code
= http
+ sizeof("HTTP/1.1");
96 info
->type
= PHP_HTTP_RESPONSE
;
97 while (' ' == *code
) ++code
;
98 if (code
&& end
> code
) {
99 PHP_HTTP_INFO(info
).response
.code
= strtol(code
, &status
, 10);
101 PHP_HTTP_INFO(info
).response
.code
= 0;
103 if (status
&& end
> status
) {
104 while (' ' == *status
) ++status
;
105 PHP_HTTP_INFO(info
).response
.status
= estrndup(status
, end
- status
);
107 PHP_HTTP_INFO(info
).response
.status
= NULL
;
114 else if (*(http
- 1) == ' ' && (!http
[lenof("HTTP/1.x")] || http
[lenof("HTTP/1.x")] == '\r' || http
[lenof("HTTP/1.x")] == '\n')) {
115 const char *url
= strchr(pre_header
, ' ');
117 info
->type
= PHP_HTTP_REQUEST
;
118 if (url
&& http
> url
) {
119 PHP_HTTP_INFO(info
).request
.method
= estrndup(pre_header
, url
- pre_header
);
120 while (' ' == *url
) ++url
;
121 while (' ' == *(http
-1)) --http
;
123 PHP_HTTP_INFO(info
).request
.url
= estrndup(url
, http
- url
);
125 STR_SET(PHP_HTTP_INFO(info
).request
.method
, NULL
);
129 PHP_HTTP_INFO(info
).request
.method
= NULL
;
130 PHP_HTTP_INFO(info
).request
.url
= NULL
;
136 /* some darn header containing HTTP/1.x */
139 php_http_info_free(&info
);
150 * vim600: noet sw=4 ts=4 fdm=marker
151 * vim<600: noet sw=4 ts=4