* added HTTPi_Request::getResponseCode()
authorMichael Wallner <mike@php.net>
Sun, 6 Mar 2005 21:43:09 +0000 (21:43 +0000)
committerMichael Wallner <mike@php.net>
Sun, 6 Mar 2005 21:43:09 +0000 (21:43 +0000)
* renamed HTTPi_Request::getResponseHeader<del>s</del>() allowing retrieval of single header values
* HTTPi_Request::getResponseInfo() allows retrieval of single info values now
* fixed overflow in http_split_response()

http.c
http_api.c
http_curl_api.c
http_methods.c
php_http.h

diff --git a/http.c b/http.c
index 7cb3c4cdf8b6d06d880777c0cbd2679b490fbdcf..9b5c7f6116165f143728bde2b8a81cd947109db3 100644 (file)
--- a/http.c
+++ b/http.c
@@ -338,7 +338,8 @@ zend_function_entry httpi_request_class_methods[] = {
        PHP_ME(HTTPi_Request, send, NULL, ZEND_ACC_PUBLIC)
 
        PHP_ME(HTTPi_Request, getResponseData, NULL, ZEND_ACC_PUBLIC)
-       PHP_ME(HTTPi_Request, getResponseHeaders, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HTTPi_Request, getResponseHeader, NULL, ZEND_ACC_PUBLIC)
+       PHP_ME(HTTPi_Request, getResponseCode, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(HTTPi_Request, getResponseBody, NULL, ZEND_ACC_PUBLIC)
        PHP_ME(HTTPi_Request, getResponseInfo, NULL, ZEND_ACC_PUBLIC)
 
index aee6785705e884759fde2c9ed3a975f8b706e4d1..2161d6d571d14f2807c022450122e174a0971e98 100644 (file)
@@ -933,7 +933,7 @@ PHP_HTTP_API char *_http_absolute_uri_ex(
 #ifdef ZEND_ENGINE_2
                if (se = getservbyname(furl.scheme, "tcp")) {
                        furl.port = ntohs(se->s_port);
-               } else 
+               } else
 #endif
                furl.port = 80;
        } else {
@@ -1469,11 +1469,11 @@ PHP_HTTP_API STATUS _http_split_response_ex(char *response,
                }
        }
 
-       if (*body && (*body_len = response_len - (*body - header))) {
-               *body = estrndup(*body, *body_len - 1);
+       if (*body && (*body_len = (response_len - (*body - header)))) {
+               *body = estrndup(*body, *body_len);
        }
 
-       return http_parse_headers_ex(header, *body ? *body - header : response_len, headers, 1);
+       return http_parse_headers_ex(header, *body ? response_len - *body_len : response_len, headers, 1);
 }
 /* }}} */
 
index 9ee8afedb7401ca21427f20404d0561e857bd7e8..08d543dbae5c82acf53c0d077ffc813eb1bb612e 100644 (file)
@@ -72,8 +72,6 @@ ZEND_DECLARE_MODULE_GLOBALS(http)
                } \
        }
 
-
-
 #define http_curl_cleanup(ch, clean_curl) \
        http_curl_freestr(); \
        http_curl_freebuf(); \
@@ -106,9 +104,9 @@ ZEND_DECLARE_MODULE_GLOBALS(http)
        HTTP_G(curlbuf).size = 0;
 
 #define http_curl_copybuf(data, size) \
-       * size = HTTP_G(curlbuf).used; \
-       * data = ecalloc(1, HTTP_G(curlbuf).used + 1); \
-       memcpy(* data, HTTP_G(curlbuf).data, * size);
+       *size = HTTP_G(curlbuf).used; \
+       *data = ecalloc(1, HTTP_G(curlbuf).used + 1); \
+       memcpy(*data, HTTP_G(curlbuf).data, HTTP_G(curlbuf).used);
 
 #define http_curl_sizebuf(for_size) \
        { \
index 45a5c61812816b6b983f639d74716bdcb66d742d..522c2a36a7c8a53c7b2f3c8aeff88847b63a3462 100644 (file)
@@ -989,7 +989,7 @@ PHP_METHOD(HTTPi_Request, unsetPostFiles)
 
 /* {{{ proto array HTTPi_Request::getResponseData()
  *
- * Get all resposonse data after sending the request.
+ * Get all resposonse data after the request has been sent.
  */
 PHP_METHOD(HTTPi_Request, getResponseData)
 {
@@ -1004,28 +1004,39 @@ PHP_METHOD(HTTPi_Request, getResponseData)
 }
 /* }}} */
 
-/* {{{ proto array HTTPi_Request::getResponseHeaders()
+/* {{{ proto string HTTPi_Request::getResponseHeader([string name])
  *
- * Get the response headers after sending the request.
+ * Get response header(s) after the request has been sent.
  */
-PHP_METHOD(HTTPi_Request, getResponseHeaders)
+PHP_METHOD(HTTPi_Request, getResponseHeader)
 {
-       zval *data, **headers;
-       getObject(httpi_request_object, obj);
-
-       NO_ARGS;
-
-       array_init(return_value);
+       zval *data, **headers, **header;
+       char *header_name = NULL;
+       int header_len = 0;
+       getObject(httpi_response_object, obj);
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
+               RETURN_FALSE;
+       }
+       
        data = GET_PROP(obj, responseData);
-       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
+       if (SUCCESS != zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
+               RETURN_FALSE;
+       }
+       
+       if (!header_len || !header_name) {
+               array_init(return_value);
                array_copy(*headers, return_value);
+       } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
+               RETURN_STRINGL(Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
+       } else {
+               RETURN_FALSE;
        }
 }
-/* }}} */
 
 /* {{{ proto string HTTPi_Request::getResponseBody()
  *
- * Get the response body after sending the request.
+ * Get the response body after the request has been sent.
  */
 PHP_METHOD(HTTPi_Request, getResponseBody)
 {
@@ -1038,26 +1049,62 @@ PHP_METHOD(HTTPi_Request, getResponseBody)
        if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
                RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
        } else {
-               Z_TYPE_P(return_value) = IS_NULL;
+               RETURN_FALSE;
        }
 }
 /* }}} */
 
-/* {{{ proto array HTTPi_Request::getResponseInfo()
+/* {{{ proto int HTTPi_Request::getResponseCode()
  *
- * Get response info after sending the request.
+ * Get the response code after the request has been sent.
+ */
+PHP_METHOD(HTTPi_Request, getResponseCode)
+{
+       zval **code, **hdrs, *data;
+       getObject(httpi_request_object, obj);
+       
+       NO_ARGS;
+       
+       data = GET_PROP(obj, responseData);
+       if (    (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &hdrs)) &&
+                       (SUCCESS == zend_hash_find(Z_ARRVAL_PP(hdrs), "Status", sizeof("Status"), (void **) &code))) {
+               RETVAL_STRINGL(Z_STRVAL_PP(code), Z_STRLEN_PP(code), 1);
+               convert_to_long(return_value);
+       } else {
+               RETURN_FALSE;
+       }
+}
+/* }}} */
+
+/* {{{ proto array HTTPi_Request::getResponseInfo([string name])
+ *
+ * Get response info after the request has been sent.
  * See http_get() for a full list of returned info.
  */
 PHP_METHOD(HTTPi_Request, getResponseInfo)
 {
-       zval *info;
+       zval *info, **infop;
+       char *info_name = NULL;
+       int info_len = 0;
        getObject(httpi_request_object, obj);
 
-       NO_ARGS;
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len)) {
+               RETURN_FALSE;
+       }
 
        info = GET_PROP(obj, responseInfo);
-       array_init(return_value);
-       array_copy(info, return_value);
+       
+       if (info_len && info_name) {
+               if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
+                       RETURN_ZVAL(*infop, 1, ZVAL_PTR_DTOR);
+               } else {
+                       php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not find response info named %s", info_name);
+                       RETURN_FALSE;
+               }
+       } else {
+               array_init(return_value);
+               array_copy(info, return_value);
+       }
 }
 /* }}}*/
 
index 447a09b1fa1c7f37e38498fdc8e2fcef8891dc93..48a7d524a8fd398194deb6684543a263044dc7be 100644 (file)
@@ -131,7 +131,8 @@ PHP_METHOD(HTTPi_Request, getPostFiles);
 PHP_METHOD(HTTPi_Request, unsetPostFiles);
 PHP_METHOD(HTTPi_Request, send);
 PHP_METHOD(HTTPi_Request, getResponseData);
-PHP_METHOD(HTTPi_Request, getResponseHeaders);
+PHP_METHOD(HTTPi_Request, getResponseHeader);
+PHP_METHOD(HTTPi_Request, getResponseCode);
 PHP_METHOD(HTTPi_Request, getResponseBody);
 PHP_METHOD(HTTPi_Request, getResponseInfo);