branch off v1 as R_1_7
[m6w6/ext-http] / http_request_api.c
index d32404ad729851a03bdc58b5bcfd95bc885b17ff..b6e40344c27001aeba102f149c63ea4b164af5a2 100644 (file)
@@ -615,7 +615,10 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
        HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
-       
+
+       if (!request->url) {
+               return FAILURE;
+       }
        if (!(storage = http_request_storage_get(request->ch))) {
                return FAILURE;
        }
@@ -846,31 +849,54 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                HashKey header_key = initHashKey(0);
                zval **header_val;
                HashPosition pos;
-
+               phpstr header;
+               
+               phpstr_init(&header);
                FOREACH_KEYVAL(pos, zoption, header_key, header_val) {
                        if (header_key.type == HASH_KEY_IS_STRING) {
-                               char header[1024];
                                zval *header_cpy = http_zsep(IS_STRING, *header_val);
                                
                                if (!strcasecmp(header_key.str, "range")) {
                                        range_req = 1;
                                }
-                               snprintf(header, sizeof(header), "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
-                               request->_cache.headers = curl_slist_append(request->_cache.headers, header);
+
+                               phpstr_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
+                               phpstr_fix(&header);
+                               request->_cache.headers = curl_slist_append(request->_cache.headers, PHPSTR_VAL(&header));
+                               phpstr_reset(&header);
+                               
                                zval_ptr_dtor(&header_cpy);
                        }
                }
+               phpstr_dtor(&header);
        }
        /* etag */
        if ((zoption = http_request_option(request, options, "etag", IS_STRING)) && Z_STRLEN_P(zoption)) {
-               char match_header[1024], *quoted_etag = NULL;
-               
-               if ((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')) {
-                       spprintf(&quoted_etag, 0, "\"%s\"", Z_STRVAL_P(zoption));
+               zend_bool is_quoted;
+               phpstr header;
+
+               phpstr_init(&header);
+               phpstr_appendf(&header, "%s: ", range_req?"If-Match":"If-None-Match");
+               if ((Z_STRVAL_P(zoption)[0] == '"') && (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] == '"')) {
+                       /* properly quoted etag */
+                       phpstr_append(&header, Z_STRVAL_P(zoption), Z_STRLEN_P(zoption));
+               } else if ((Z_STRVAL_P(zoption)[0] == 'W') && (Z_STRVAL_P(zoption)[1] == '/')) {
+                       /* weak etag */
+                       if ((Z_STRLEN_P(zoption) > 3) && (Z_STRVAL_P(zoption)[2] == '"') && (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] == '"')) {
+                               /* quoted */
+                               phpstr_append(&header, Z_STRVAL_P(zoption), Z_STRLEN_P(zoption));
+                       } else {
+                               /* unquoted */
+                               phpstr_appendf(&header, "W/\"%s\"", Z_STRVAL_P(zoption) + 2);
+                       }
+               } else {
+                       /* assume unquoted etag */
+                       phpstr_appendf(&header, "\"%s\"", Z_STRVAL_P(zoption));
                }
-               snprintf(match_header, sizeof(match_header), "%s: %s", range_req?"If-Match":"If-None-Match", quoted_etag?quoted_etag:Z_STRVAL_P(zoption));
-               request->_cache.headers = curl_slist_append(request->_cache.headers, match_header);
-               STR_FREE(quoted_etag);
+               phpstr_fix(&header);
+
+               request->_cache.headers = curl_slist_append(request->_cache.headers, PHPSTR_VAL(&header));
+               phpstr_dtor(&header);
        }
        /* compression */
        if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {