X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_send_api.c;h=0c4306f86afd130eac668d076423f58cdc3f7ed8;hp=6dfc677f993b5be0eef58657c291506311b986c8;hb=100b5ff381fe93e7ac4d7b0ea68f9b7256615e68;hpb=a60610ff0f84cdf46d8b39c9e64eefd701c1b565 diff --git a/http_send_api.c b/http_send_api.c index 6dfc677..0c4306f 100644 --- a/http_send_api.c +++ b/http_send_api.c @@ -156,18 +156,22 @@ PHP_HTTP_API STATUS _http_send_header_ex(const char *name, size_t name_len, cons char *header = emalloc(header_len + 1); header[header_len] = '\0'; - snprintf(header, header_len, "%s: %s", name, value); - ret = http_send_header_string_ex(header, replace); - efree(header); + header_len = snprintf(header, header_len, "%s: %s", name, value); + ret = http_send_header_string_ex(header, header_len, replace); + if (sent_header) { + *sent_header = header; + } else { + efree(header); + } return ret; } /* }}} */ /* {{{ STATUS http_send_status_header(int, char *) */ -PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, zend_bool replace TSRMLS_DC) +PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, size_t header_len, zend_bool replace TSRMLS_DC) { STATUS ret; - sapi_header_line h = {(char *) header, header ? strlen(header) : 0, status}; + sapi_header_line h = {(char *) header, header_len, status}; if (SUCCESS != (ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, &h TSRMLS_CC))) { http_error_ex(HE_WARNING, HTTP_E_HEADER, "Could not send header: %s (%d)", header, status); } @@ -185,7 +189,7 @@ PHP_HTTP_API STATUS _http_send_last_modified_ex(time_t t, char **sent_header TSR return FAILURE; } - ret = http_send_header_ex("Last-Modified", lenof("Last-Modifed"), date, strlen(date), 1, sent_header); + ret = http_send_header_ex("Last-Modified", lenof("Last-Modified"), date, strlen(date), 1, sent_header); efree(date); /* remember */ @@ -207,12 +211,10 @@ PHP_HTTP_API STATUS _http_send_etag_ex(const char *etag, size_t etag_len, char * } /* remember */ - STR_FREE(HTTP_G(send).unquoted_etag); - HTTP_G(send).unquoted_etag = estrdup(etag); + STR_SET(HTTP_G(send).unquoted_etag, estrndup(etag, etag_len)); - etag_header = ecalloc(1, sizeof("ETag: \"\"") + etag_len); - sprintf(etag_header, "ETag: \"%s\"", etag); - status = http_send_header_string(etag_header); + etag_len = spprintf(&etag_header, 0, "ETag: \"%s\"", etag); + status = http_send_header_string_ex(etag_header, etag_len, 1); if (sent_header) { *sent_header = etag_header; @@ -227,10 +229,7 @@ PHP_HTTP_API STATUS _http_send_etag_ex(const char *etag, size_t etag_len, char * /* {{{ STATUS http_send_content_type(char *, size_t) */ PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC) { - if (!strchr(content_type, '/')) { - http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to consist of a primary and a secondary part", content_type); - return FAILURE; - } + HTTP_CHECK_CONTENT_TYPE(content_type, return FAILURE); /* remember for multiple ranges */ STR_FREE(HTTP_G(send).content_type); @@ -289,6 +288,7 @@ PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_ /* multi range */ else { + size_t preface_len; char bound[23] = {0}, preface[1024] = {0}, multi_header[68] = "Content-Type: multipart/byteranges; boundary="; @@ -307,7 +307,7 @@ PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_ break; } - snprintf(preface, 1023, + preface_len = snprintf(preface, 1023, HTTP_CRLF "%s" HTTP_CRLF "Content-Type: %s" HTTP_CRLF "Content-Range: bytes %ld-%ld/%lu" @@ -321,7 +321,7 @@ PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_ (ulong) size ); - PHPWRITE(preface, strlen(preface)); + PHPWRITE(preface, preface_len); http_send_chunk(data, Z_LVAL_PP(zbegin), Z_LVAL_PP(zend) + 1, mode); } @@ -350,10 +350,7 @@ PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_s } /* stop on-the-fly etag generation */ - if (cache_etag = HTTP_G(etag).started) { - /* interrupt ob_etaghandler */ - HTTP_G(etag).started = 0; - } + cache_etag = http_interrupt_ob_etaghandler(); /* enable partial dl and resume */ http_send_header_string("Accept-Ranges: bytes"); @@ -382,13 +379,11 @@ PHP_HTTP_API STATUS _http_send_ex(const void *data_ptr, size_t data_size, http_s /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */ if (!no_cache && cache_etag) { char *etag = NULL; - - if (!(etag = http_etag(data_ptr, data_size, data_mode))) { - http_error(HE_NOTICE, HTTP_E_RUNTIME, "Failed to generate ETag for data source"); - } else { + + if (etag = http_etag(data_ptr, data_size, data_mode)) { char *sent_header = NULL; - http_send_etag_ex(etag, 32, &sent_header); + http_send_etag_ex(etag, strlen(etag), &sent_header); if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) { return http_exit_ex(304, sent_header, NULL, 0); } else {