/* $Id$ */
-#define _WINSOCKAPI_
-#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
-
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <ctype.h>
+#ifdef PHP_WIN32
+# define _WINSOCKAPI_
+# include <winsock2.h>
+#elif defined(HAVE_NETDB_H)
+# include <netdb.h>
+#endif
+
#include "php.h"
#include "php_version.h"
#include "php_streams.h"
PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC)
{
struct tm *gmtime, tmbuf;
- char *date = ecalloc(1, 31);
-
- gmtime = php_gmtime_r(&t, &tmbuf);
- snprintf(date, 30,
- "%s, %02d %s %04d %02d:%02d:%02d GMT",
- days[gmtime->tm_wday], gmtime->tm_mday,
- months[gmtime->tm_mon], gmtime->tm_year + 1900,
- gmtime->tm_hour, gmtime->tm_min, gmtime->tm_sec
- );
- return date;
+
+ if (gmtime = php_gmtime_r(&t, &tmbuf)) {
+ char *date = ecalloc(1, 31);
+ snprintf(date, 30,
+ "%s, %02d %s %04d %02d:%02d:%02d GMT",
+ days[gmtime->tm_wday], gmtime->tm_mday,
+ months[gmtime->tm_mon], gmtime->tm_year + 1900,
+ gmtime->tm_hour, gmtime->tm_min, gmtime->tm_sec
+ );
+ return date;
+ }
+
+ return NULL;
}
/* }}} */
/* a digit */
int val;
char *end;
- if((seconds == -1) && (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
+ if ((seconds == -1) &&
+ (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
/* time stamp! */
date += 8;
found = 1;
else {
val = (int) strtol(date, &end, 10);
- if ((tz_offset == -1) && ((end - date) == 4) && (val < 1300) && (indate < date) && ((date[-1] == '+' || date[-1] == '-'))) {
+ if ((tz_offset == -1) && ((end - date) == 4) && (val < 1300) &&
+ (indate < date) && ((date[-1] == '+' || date[-1] == '-'))) {
/* four digits and a value less than 1300 and it is preceeded with
a plus or minus. This is a time zone indication. */
found = 1;
}
/* }}} */
-/* {{{ int http_is_range_request(void) */
-PHP_HTTP_API int _http_is_range_request(TSRMLS_D)
-{
- return zend_hash_exists(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
- "HTTP_RANGE", sizeof("HTTP_RANGE"));
-}
-/* }}} */
-
-/* {{{ STATUS http_send_status(int) */
-PHP_HTTP_API STATUS _http_send_status(const int status TSRMLS_DC)
-{
- int s = status;
- return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC);
-}
-/* }}} */
-
-/* {{{ STATUS http_send_header(char *) */
-PHP_HTTP_API STATUS _http_send_header(const char *header TSRMLS_DC)
-{
- return http_send_status_header(0, header);
-}
-/* }}} */
-
/* {{{ STATUS http_send_status_header(int, char *) */
PHP_HTTP_API STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC)
{
/* {{{ STATUS http_send_last_modified(int) */
PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
{
- char modified[96] = "Last-Modified: ", *date;
- date = http_date(t);
- strcat(modified, date);
- efree(date);
+ char *date = NULL;
+ if (date = http_date(t)) {
+ char modified[96] = "Last-Modified: ";
+ strcat(modified, date);
+ efree(date);
- /* remember */
- HTTP_G(lmod) = t;
+ /* remember */
+ HTTP_G(lmod) = t;
- return http_send_header(modified);
+ return http_send_header(modified);
+ }
+ return FAILURE;
}
/* }}} */
}
/* }}} */
-/* {{{ char *http_absolute_uri(char *, char *) */
-PHP_HTTP_API char *_http_absolute_uri(const char *url,
- const char *proto TSRMLS_DC)
+/* {{{ char *http_absolute_uri(char *) */
+PHP_HTTP_API char *_http_absolute_uri_ex(
+ const char *url, size_t url_len,
+ const char *proto, size_t proto_len,
+ const char *host, size_t host_len,
+ unsigned port TSRMLS_DC)
{
- char *proto_ptr, *host, *path, *PTR, *URI = ecalloc(1, HTTP_URI_MAXLEN + 1);
- zval *zhost;
+#if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
+ struct servent *se;
+#endif
+ php_url *purl, furl = {NULL};
+ size_t full_len = 0;
+ zval *zhost = NULL;
+ char *scheme = NULL, *URL = ecalloc(1, HTTP_URI_MAXLEN + 1);
+
+ if ((!url || !url_len) && (
+ (!(url = SG(request_info).request_uri)) ||
+ (!(url_len = strlen(SG(request_info).request_uri))))) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING,
+ "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
+ return NULL;
+ }
- if (!url || !strlen(url)) {
- if (!SG(request_info).request_uri) {
- return NULL;
- }
- url = SG(request_info).request_uri;
+ if (!(purl = php_url_parse(url))) {
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse supplied URL");
+ return NULL;
}
- /* Mess around with already absolute URIs */
- else if (proto_ptr = strstr(url, "://")) {
- if (!proto || !strncmp(url, proto, strlen(proto))) {
- strncpy(URI, url, HTTP_URI_MAXLEN);
- return URI;
- } else {
- snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
- return URI;
+
+ furl.user = purl->user;
+ furl.pass = purl->pass;
+ furl.path = purl->path;
+ furl.query = purl->query;
+ furl.fragment = purl->fragment;
+
+ if (proto) {
+ furl.scheme = scheme = estrdup(proto);
+ } else if (purl->scheme) {
+ furl.scheme = purl->scheme;
+#if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
+ } else if (port && (se = getservbyport(port, "tcp"))) {
+ furl.scheme = (scheme = estrdup(se->s_name));
+#endif
+ } else {
+ furl.scheme = "http";
+ }
+
+ if (port) {
+ furl.port = port;
+ } else if (purl->port) {
+ furl.port = purl->port;
+ } else if (strncmp(furl.scheme, "http", 4)) {
+#if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
+ if (se = getservbyname(furl.scheme, "tcp")) {
+ furl.port = se->s_port;
+ } else
+#endif
+ furl.port = 80;
+ } else {
+ furl.port = (furl.scheme[5] == 's') ? 443 : 80;
+ }
+
+ if (host) {
+ furl.host = (char *) host;
+ } else if (purl->host) {
+ furl.host = purl->host;
+ } else if ( (zhost = http_get_server_var("HTTP_HOST")) ||
+ (zhost = http_get_server_var("SERVER_NAME"))) {
+ furl.host = Z_STRVAL_P(zhost);
+ } else {
+ furl.host = "localhost";
+ }
+
+#define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
+#define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
+#define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
+ if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
+ php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
+ "Absolute URI would have exceeded max URI length (%d bytes) - " \
+ "tried to add %d bytes ('%s')", \
+ HTTP_URI_MAXLEN, add_len, add_string); \
+ if (scheme) { \
+ efree(scheme); \
+ } \
+ php_url_free(purl); \
+ return URL; \
+ } else { \
+ strcat(URL, add_string); \
+ }
+
+ HTTP_URI_STRLCATL(URL, full_len, furl.scheme);
+ HTTP_URI_STRLCATS(URL, full_len, "://");
+
+ if (furl.user) {
+ HTTP_URI_STRLCATL(URL, full_len, furl.user);
+ if (furl.pass) {
+ HTTP_URI_STRLCATS(URL, full_len, ":");
+ HTTP_URI_STRLCATL(URL, full_len, furl.pass);
}
+ HTTP_URI_STRLCATS(URL, full_len, "@");
}
- /* protocol defaults to http */
- if (!proto || !strlen(proto)) {
- proto = "http";
+ HTTP_URI_STRLCATL(URL, full_len, furl.host);
+
+ if ( (!strcmp(furl.scheme, "http") && (furl.port != 80)) ||
+ (!strcmp(furl.scheme, "https") && (furl.port != 443))) {
+ char port_string[8] = {0};
+ snprintf(port_string, 7, ":%u", furl.port);
+ HTTP_URI_STRLCATL(URL, full_len, port_string);
}
- /* get host name */
- if ( (zhost = http_get_server_var("HTTP_HOST")) ||
- (zhost = http_get_server_var("SERVER_NAME"))) {
- host = Z_STRVAL_P(zhost);
+ if (furl.path) {
+ if (furl.path[0] != '/') {
+ HTTP_URI_STRLCATS(URL, full_len, "/");
+ }
+ HTTP_URI_STRLCATL(URL, full_len, furl.path);
} else {
- host = "localhost";
+ HTTP_URI_STRLCATS(URL, full_len, "/");
}
+ if (furl.query) {
+ HTTP_URI_STRLCATS(URL, full_len, "?");
+ HTTP_URI_STRLCATL(URL, full_len, furl.query);
+ }
- /* glue together */
- if (url[0] == '/') {
- snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s", proto, host, url);
- } else if (SG(request_info).request_uri) {
- path = estrdup(SG(request_info).request_uri);
- php_dirname(path, strlen(path));
- snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s/%s", proto, host, path, url);
- efree(path);
- } else {
- snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
+ if (furl.fragment) {
+ HTTP_URI_STRLCATS(URL, full_len, "#");
+ HTTP_URI_STRLCATL(URL, full_len, furl.fragment);
}
- /* strip everything after a new line */
- if ((PTR = strchr(URI, '\r')) || (PTR = strchr(URI, '\n'))) {
- PTR = 0;
+ if (scheme) {
+ efree(scheme);
}
+ php_url_free(purl);
- return URI;
+ return URL;
}
/* }}} */
}
/* }}} */
-/* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
-PHP_HTTP_API http_range_status _http_get_request_ranges(zval *zranges,
+/* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
+PHP_HTTP_API http_range_status _http_get_request_ranges(HashTable *ranges,
const size_t length TSRMLS_DC)
{
zval *zrange;
array_init(zentry);
add_index_long(zentry, 0, begin);
add_index_long(zentry, 1, end);
- add_next_index_zval(zranges, zentry);
+ zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
begin = -1;
end = -1;
}
/* }}} */
-/* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
-PHP_HTTP_API STATUS _http_send_ranges(zval *zranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
+/* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
+PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
{
- int c;
long **begin, **end;
zval **zrange;
- /* Send HTTP 206 Partial Content */
- http_send_status(206);
-
/* single range */
- if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
+ if (zend_hash_num_elements(ranges) == 1) {
char range_header[256] = {0};
- zend_hash_index_find(Z_ARRVAL_P(zranges), 0, (void **) &zrange);
- zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin);
- zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end);
+ if (SUCCESS != zend_hash_index_find(ranges, 0, (void **) &zrange) ||
+ SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
+ SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
+ return FAILURE;
+ }
+
+ /* Send HTTP 206 Partial Content */
+ http_send_status(206);
/* send content range header */
snprintf(range_header, 255, "Content-Range: bytes %d-%d/%d", **begin, **end, size);
/* multi range */
else {
- int i;
char bound[23] = {0}, preface[1024] = {0},
multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
+ /* Send HTTP 206 Partial Content */
+ http_send_status(206);
+
+ /* send multipart/byteranges header */
snprintf(bound, 22, "--%d%0.9f", time(NULL), php_combined_lcg(TSRMLS_C));
strncat(multi_header, bound + 2, 21);
http_send_header(multi_header);
/* send each requested chunk */
- for ( i = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges));
- i < c;
- i++, zend_hash_move_forward(Z_ARRVAL_P(zranges))) {
- if ( HASH_KEY_NON_EXISTANT == zend_hash_get_current_data(
- Z_ARRVAL_P(zranges), (void **) &zrange) ||
- SUCCESS != zend_hash_index_find(
- Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
- SUCCESS != zend_hash_index_find(
- Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
+ FOREACH_HASH_VAL(ranges, zrange) {
+ if (SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
+ SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
break;
}
}
/* write boundary once more */
- php_body_write(HTTP_CRLF, 2 TSRMLS_CC);
+ php_body_write(HTTP_CRLF, sizeof(HTTP_CRLF) - 1 TSRMLS_CC);
php_body_write(bound, strlen(bound) TSRMLS_CC);
+ php_body_write("--", 2 TSRMLS_CC);
return SUCCESS;
}
}
/* }}} */
-/* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
+/* {{{ STATUS http_send(void *, size_t, http_send_mode) */
PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size,
const http_send_mode data_mode TSRMLS_DC)
{
if (!data_ptr) {
return FAILURE;
}
+ if (!data_size) {
+ return SUCCESS;
+ }
/* etag handling */
if (HTTP_G(etag_started)) {
) {
STATUS result = FAILURE;
- zval *zranges = NULL;
- MAKE_STD_ZVAL(zranges);
- array_init(zranges);
+ HashTable ranges;
+ zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
- switch (http_get_request_ranges(zranges, data_size))
+ switch (http_get_request_ranges(&ranges, data_size))
{
case RANGE_NO:
- zval_dtor(zranges);
- efree(zranges);
+ zend_hash_destroy(&ranges);
/* go ahead and send all */
break;
case RANGE_OK:
- result = http_send_ranges(zranges, data_ptr, data_size, data_mode);
- zval_dtor(zranges);
- efree(zranges);
+ result = http_send_ranges(&ranges, data_ptr, data_size, data_mode);
+ zend_hash_destroy(&ranges);
return result;
break;
case RANGE_ERR:
- zval_dtor(zranges);
- efree(zranges);
+ zend_hash_destroy(&ranges);
http_send_status(416);
return FAILURE;
break;
}
/* }}} */
-/* {{{ STATUS http_send_data(zval *) */
-PHP_HTTP_API STATUS _http_send_data(const zval *zdata TSRMLS_DC)
-{
- if (!Z_STRLEN_P(zdata)) {
- return SUCCESS;
- }
- if (!Z_STRVAL_P(zdata)) {
- return FAILURE;
- }
-
- return http_send(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), SEND_DATA);
-}
-/* }}} */
-
/* {{{ STATUS http_send_stream(php_stream *) */
-PHP_HTTP_API STATUS _http_send_stream(const php_stream *file TSRMLS_DC)
-{
- if (php_stream_stat((php_stream *) file, &HTTP_G(ssb))) {
- return FAILURE;
- }
-
- return http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
-}
-/* }}} */
-
-/* {{{ STATUS http_send_file(zval *) */
-PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC)
+PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file,
+ zend_bool close_stream TSRMLS_DC)
{
- php_stream *file;
- STATUS ret;
+ STATUS status;
- if (!Z_STRLEN_P(zfile)) {
+ if ((!file) || php_stream_stat(file, &HTTP_G(ssb))) {
return FAILURE;
}
- if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
- REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
- return FAILURE;
+ status = http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
+
+ if (close_stream) {
+ php_stream_close(file);
}
- ret = http_send_stream(file);
- php_stream_close(file);
- return ret;
+ return status;
}
/* }}} */
-/* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
+/* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
{
}
/* }}} */
-/* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
-PHP_HTTP_API STATUS _http_split_response_ex( char *response,
- size_t response_len, zval *zheaders, zval *zbody TSRMLS_DC)
+/* {{{ STATUS http_split_response(zval *, zval *, zval *) */
+PHP_HTTP_API STATUS _http_split_response(zval *response, zval *headers, zval *body TSRMLS_DC)
+{
+ char *b = NULL;
+ size_t l = 0;
+ STATUS status = http_split_response_ex(Z_STRVAL_P(response), Z_STRLEN_P(response), Z_ARRVAL_P(headers), &b, &l);
+ ZVAL_STRINGL(body, b, l, 0);
+ return status;
+}
+/* }}} */
+
+/* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
+PHP_HTTP_API STATUS _http_split_response_ex(char *response,
+ size_t response_len, HashTable *headers, char **body, size_t *body_len TSRMLS_DC)
{
- char *body = NULL;
- char *header = response;
+ char *header = response, *real_body = NULL;
- while ((response - header + 4) < response_len) {
+ while (0 < (response_len - (response - header + 4))) {
if ( (*response++ == '\r') &&
(*response++ == '\n') &&
(*response++ == '\r') &&
(*response++ == '\n')) {
- body = response;
+ real_body = response;
break;
}
}
- if (body && (response_len - (body - header))) {
- ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
- } else {
- Z_TYPE_P(zbody) = IS_NULL;
+ if (real_body && (*body_len = (response_len - (real_body - header)))) {
+ *body = ecalloc(1, *body_len + 1);
+ memcpy(*body, real_body, *body_len);
}
- return http_parse_headers(header, body ? body - header : response_len, zheaders);
+ return http_parse_headers_ex(header, real_body ? response_len - *body_len : response_len, headers, 1);
}
/* }}} */
/* {{{ STATUS http_parse_headers(char *, long, zval *) */
-PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *array TSRMLS_DC)
+PHP_HTTP_API STATUS _http_parse_headers_ex(char *header, int header_len,
+ HashTable *headers, zend_bool prettify TSRMLS_DC)
{
char *colon = NULL, *line = NULL, *begin = header;
+ zval array;
+
+ Z_ARRVAL(array) = headers;
if (header_len < 2) {
return FAILURE;
/* status code */
if (!strncmp(header, "HTTP/1.", 7)) {
char *end = strstr(header, HTTP_CRLF);
- add_assoc_stringl(array, "Status",
+ add_assoc_stringl(&array, "Status",
header + sizeof("HTTP/1.x ") - 1,
end - (header + sizeof("HTTP/1.x ") - 1), 1);
header = end + 2;
/* skip empty key */
if (header != colon) {
char *key = estrndup(header, colon - header);
+
+ if (prettify) {
+ key = pretty_key(key, colon - header, 1, 1);
+ }
+
value_len += line - colon - 1;
/* skip leading ws */
if (value_len < 1) {
/* hm, empty header? */
- add_assoc_stringl(array, key, "", 0, 1);
+ add_assoc_stringl(&array, key, "", 0, 1);
} else {
- add_assoc_stringl(array, key, colon, value_len, 1);
+ add_assoc_stringl(&array, key, colon, value_len, 1);
}
efree(key);
}
}
/* }}} */
-/* {{{ void http_get_request_headers(zval *) */
-PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
+/* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
+PHP_HTTP_API void _http_get_request_headers_ex(HashTable *headers, zend_bool prettify TSRMLS_DC)
{
char *key = NULL;
long idx = 0;
+ zval array;
+
+ Z_ARRVAL(array) = headers;
FOREACH_HASH_KEY(HTTP_SERVER_VARS, key, idx) {
if (key && !strncmp(key, "HTTP_", 5)) {
zval **header;
+
+ if (prettify) {
+ key = pretty_key(key + 5, strlen(key) - 5, 1, 1);
+ }
+
zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
- add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
+ add_assoc_stringl(&array, key, Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
+ key = NULL;
}
}
}
/* }}} */
#ifndef ZEND_ENGINE_2
-/* {{{ php_url_encode_hash
- Author: Sarah Golemon <pollita@php.net> */
-PHP_HTTP_API int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
+/* {{{ php_url_encode_hash
+ Author: Sara Golemon <pollita@php.net> */
+PHP_HTTP_API STATUS php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
const char *num_prefix, int num_prefix_len,
const char *key_prefix, int key_prefix_len,
const char *key_suffix, int key_suffix_len,