* separate http_parse_header() function (doesn't support folded headers yet)
authorMichael Wallner <mike@php.net>
Wed, 9 Feb 2005 20:10:20 +0000 (20:10 +0000)
committerMichael Wallner <mike@php.net>
Wed, 9 Feb 2005 20:10:20 +0000 (20:10 +0000)
http.c
http_api.c
php_http.h
php_http_api.h

diff --git a/http.c b/http.c
index e432ef02abc5730e3df1648c7f9ec8f93b3c185d..e7e1960e3b5b5d427e643a0fa19e882837224cb2 100644 (file)
--- a/http.c
+++ b/http.c
@@ -71,6 +71,7 @@ function_entry http_functions[] = {
        PHP_FE(http_send_stream, NULL)
        PHP_FE(http_chunked_decode, NULL)
        PHP_FE(http_split_response, NULL)
+       PHP_FE(http_parse_header, NULL)
 #ifdef HTTP_HAVE_CURL
        PHP_FE(http_get, NULL)
        PHP_FE(http_head, NULL)
@@ -677,7 +678,10 @@ PHP_FUNCTION(http_split_response)
        MAKE_STD_ZVAL(zheaders);
        array_init(zheaders);
 
-       http_split_response(zresponse, zheaders, zbody);
+       if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
+               RETURN_FALSE;
+       }
 
        array_init(return_value);
        add_index_zval(return_value, 0, zheaders);
@@ -685,6 +689,29 @@ PHP_FUNCTION(http_split_response)
 }
 /* }}} */
 
+/* {{{ proto array http_parse_header(string header) */
+PHP_FUNCTION(http_parse_header)
+{
+       char *header, *rnrn;
+       int header_len;
+
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
+               RETURN_FALSE;
+       }
+       
+       array_init(return_value);
+       
+       if (rnrn = strstr(header, "\r\n\r\n")) {
+               header_len = rnrn - header + 2;
+       }
+       if (SUCCESS != http_parse_header(header, header_len, return_value)) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
+               zval_dtor(return_value);
+               RETURN_FALSE;
+       }
+}
+/* }}}*/
+
 /* {{{ HAVE_CURL */
 #ifdef HTTP_HAVE_CURL
 
index 9cb81bace763d597f2764240f39f8bba021d86c4..f68f57e3d6aed62b6a6fec835f46e94ae3b1b286 100644 (file)
@@ -1669,8 +1669,8 @@ PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
 }
 /* }}} */
 
-/* {{{ proto void http_split_response(zval *, zval *, zval *) */
-PHP_HTTP_API void _http_split_response(const zval *zresponse, zval *zheaders,
+/* {{{ proto STATUS http_split_response(zval *, zval *, zval *) */
+PHP_HTTP_API STATUS _http_split_response(const zval *zresponse, zval *zheaders,
        zval *zbody TSRMLS_DC)
 {
        char *header, *response, *body = NULL;
@@ -1693,42 +1693,56 @@ PHP_HTTP_API void _http_split_response(const zval *zresponse, zval *zheaders,
                Z_TYPE_P(zbody) = IS_NULL;
        }
 
-       /* check for HTTP status - FIXXME: strchr() */
+       return http_parse_header(header, body - Z_STRVAL_P(zresponse), zheaders);
+}
+/* }}} */
+
+/* {{{ STATUS http_parse_header(char *, long, zval *) */
+PHP_HTTP_API STATUS _http_parse_header(char *header, long header_len, zval *array TSRMLS_DC)
+{
+       char *colon = NULL, *line = NULL, *begin = header;
+
+       if (header_len < 8) {
+               return FAILURE;
+       }
+
+       /* status code */
        if (!strncmp(header, "HTTP/1.", 7)) {
-               char *end = strchr(header, '\r');
-               add_assoc_stringl(zheaders, "Status",
+               char *end = strstr(header, "\r\n");
+               add_assoc_stringl(array, "Status",
                        header + strlen("HTTP/1.x "),
                        end - (header + strlen("HTTP/1.x ")), 1);
                header = end + 2;
        }
-       /* split headers */
-       {
-               char *colon = NULL, *line = header;
 
-               while ( (line - Z_STRVAL_P(zresponse) + 3) <
-                               (body - Z_STRVAL_P(zresponse))) {
-                       switch (*line++)
-                       {
-                               case '\r':
-                                       if (colon && (*line == '\n')) {
-                                               char *key = estrndup(header, colon - header);
-                                               add_assoc_stringl(zheaders, key,
-                                                       colon + 2, line - colon - 3, 1);
-                                               efree(key);
-
-                                               colon = NULL;
-                                               header += line - header + 1;
-                                       }
-                               break;
+       line = header;
 
-                               case ':':
-                                       if (!colon) {
-                                               colon = line - 1;
-                                       }
-                               break;
-                       }
+       /*
+        * FIXXME: support for folded headers
+        */
+       while (header_len > (line - begin)) {
+               switch (*line++)
+               {
+                       case 0:
+                       case '\r':
+                               if (colon && (*line == '\n')) {
+                                       char *key = estrndup(header, colon - header);
+                                       add_assoc_stringl(array, key, colon + 2, line - colon - 3, 1);
+                                       efree(key);
+
+                                       colon = NULL;
+                                       header += line - header + 1;
+                               }
+                       break;
+
+                       case ':':
+                               if (!colon) {
+                                       colon = line - 1;
+                               }
+                       break;
                }
        }
+       return SUCCESS;
 }
 /* }}} */
 
index cf955a84040c933ec8f0bb673dec90c77eab2a9a..d6ac71abcf4db293e40cde168beabde5c0ed4767 100644 (file)
@@ -56,6 +56,7 @@ PHP_FUNCTION(http_send_file);
 PHP_FUNCTION(http_send_stream);
 PHP_FUNCTION(http_chunked_decode);
 PHP_FUNCTION(http_split_response);
+PHP_FUNCTION(http_parse_header);
 #ifdef HTTP_HAVE_CURL
 PHP_FUNCTION(http_get);
 PHP_FUNCTION(http_head);
index 61ae827784c3101d1341983f7f093bf82b178773..de7714c34a3a667d3ea5011990e48f0605762408 100644 (file)
@@ -150,7 +150,10 @@ PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC);
 PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded, const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC);
 
 #define http_split_response(r, h, b) _http_split_response((r), (h), (b) TSRMLS_CC)
-PHP_HTTP_API void _http_split_response(const zval *zresponse, zval *zheaders, zval *zbody TSRMLS_DC);
+PHP_HTTP_API STATUS _http_split_response(const zval *zresponse, zval *zheaders, zval *zbody TSRMLS_DC);
+
+#define http_parse_header(h, l, a) _http_parse_header((h), (l), (a) TSRMLS_CC)
+PHP_HTTP_API STATUS _http_parse_header(char *header, long header_len, zval *array TSRMLS_DC);
 
 /* {{{ HAVE_CURL */
 #ifdef HTTP_HAVE_CURL