-
- date = end;
- }
- }
-
- part++;
- }
-
- if (-1 == seconds) {
- seconds = minutes = hours = 0; /* no time, make it zero */
- }
-
- if ((-1 == monthday) || (-1 == month) || (-1 == year)) {
- /* lacks vital info, fail */
- return -1;
- }
-
- if (sizeof(time_t) < 5) {
- /* 32 bit time_t can only hold dates to the beginning of 2038 */
- if (year > 2037) {
- return 0x7fffffff;
- }
- }
-
- tm.tm_sec = seconds;
- tm.tm_min = minutes;
- tm.tm_hour = hours;
- tm.tm_mday = monthday;
- tm.tm_mon = month;
- tm.tm_year = year - 1900;
- tm.tm_wday = 0;
- tm.tm_yday = 0;
- tm.tm_isdst = 0;
-
- t = mktime(&tm);
-
- /* time zone adjust */
- {
- struct tm *gmt, keeptime2;
- long delta;
- time_t t2;
-
- if(!(gmt = php_gmtime_r(&t, &keeptime2))) {
- return -1; /* illegal date/time */
- }
-
- t2 = mktime(gmt);
-
- /* Add the time zone diff (between the given timezone and GMT) and the
- diff between the local time zone and GMT. */
- delta = (tz_offset != -1 ? tz_offset : 0) + (t - t2);
-
- if((delta > 0) && (t + delta < t)) {
- return -1; /* time_t overflow */
- }
-
- t += delta;
- }
-
- return t;
-}
-/* }}} */
-
-/* {{{ inline STATUS http_send_status(int) */
-PHP_HTTP_API inline STATUS _http_send_status(const int status TSRMLS_DC)
-{
- int s = status;
- return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC);
-}
-/* }}} */
-
-/* {{{ inline STATUS http_send_header(char *) */
-PHP_HTTP_API inline STATUS _http_send_header(const char *header TSRMLS_DC)
-{
- return http_send_status_header(0, header);
-}
-/* }}} */
-
-/* {{{ inline STATUS http_send_status_header(int, char *) */
-PHP_HTTP_API inline STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC)
-{
- sapi_header_line h = {(char *) header, strlen(header), status};
- return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC);
-}
-/* }}} */
-
-/* {{{ inline zval *http_get_server_var(char *) */
-PHP_HTTP_API inline zval *_http_get_server_var(const char *key TSRMLS_DC)
-{
- zval **var;
- if (SUCCESS == zend_hash_find(
- HTTP_SERVER_VARS,
- (char *) key, strlen(key) + 1, (void **) &var)) {
- return *var;
- }
- return NULL;
-}
-/* }}} */
-
-/* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
-PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
- char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
-{
- char etag[33] = { 0 };
- unsigned char digest[16];
-
- if (mode & PHP_OUTPUT_HANDLER_START) {
- PHP_MD5Init(&HTTP_G(etag_md5));
- }
-
- PHP_MD5Update(&HTTP_G(etag_md5), output, output_len);
-
- if (mode & PHP_OUTPUT_HANDLER_END) {
- PHP_MD5Final(digest, &HTTP_G(etag_md5));
-
- /* just do that if desired */
- if (HTTP_G(etag_started)) {
- make_digest(etag, digest);
-
- if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
- http_send_status(304);
- } else {
- http_send_etag(etag, 32);
- }
- }
- }
-
- *handled_output_len = output_len;
- *handled_output = estrndup(output, output_len);
-}
-/* }}} */
-
-/* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
-PHP_HTTP_API STATUS _http_start_ob_handler(php_output_handler_func_t handler_func,
- char *handler_name, uint chunk_size, zend_bool erase TSRMLS_DC)
-{
- php_ob_buffer **stack;
- int count, i;
-
- if (count = OG(ob_nesting_level)) {
- stack = ecalloc(sizeof(php_ob_buffer), count);
-
- if (count > 1) {
- zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
- (int (*)(void *elem, void *)) http_ob_stack_get, stack);
- }
-
- if (count > 0) {
- http_ob_stack_get(&OG(active_ob_buffer), stack);
- }
-
- while (OG(ob_nesting_level)) {
- php_end_ob_buffer(0, 0 TSRMLS_CC);
- }
- }
-
- php_ob_set_internal_handler(handler_func, chunk_size, handler_name, erase TSRMLS_CC);
-
- for (i = 0; i < count; i++) {
- php_ob_buffer *s = stack[i];
- if (strcmp(s->handler_name, "default output handler")) {
- php_start_ob_buffer_named(s->handler_name, s->chunk_size, s->erase TSRMLS_CC);
- }
- php_body_write(s->buffer, s->text_length TSRMLS_CC);
- }
-
- return SUCCESS;
-}
-/* }}} */
-
-/* {{{ int http_modified_match(char *, int) */
-PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
-{
- int retval;
- zval *zmodified;
- char *modified, *chr_ptr;
-
- HTTP_GSC(zmodified, entry, 0);
-
- modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
- if (chr_ptr = strrchr(modified, ';')) {
- chr_ptr = 0;
- }
- retval = (t <= http_parse_date(modified));
- efree(modified);
- return retval;
-}
-/* }}} */
-
-/* {{{ int http_etag_match(char *, char *) */
-PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
-{
- zval *zetag;
- char *quoted_etag;
- STATUS result;
-
- HTTP_GSC(zetag, entry, 0);
-
- if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
- return 1;
- }
-
- quoted_etag = (char *) emalloc(strlen(etag) + 3);
- sprintf(quoted_etag, "\"%s\"", etag);
-
- if (!strchr(Z_STRVAL_P(zetag), ',')) {
- result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
- } else {
- result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
- }
- efree(quoted_etag);
- return result;
-}
-/* }}} */
-
-/* {{{ 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);
-
- /* remember */
- HTTP_G(lmod) = t;
-
- return http_send_header(modified);
-}
-/* }}} */
-
-/* {{{ static STATUS http_send_etag(char *, int) */
-PHP_HTTP_API STATUS _http_send_etag(const char *etag,
- const int etag_len TSRMLS_DC)
-{
- STATUS ret;
- int header_len;
- char *etag_header;
-
- header_len = sizeof("ETag: \"\"") + etag_len + 1;
- etag_header = ecalloc(header_len, 1);
- sprintf(etag_header, "ETag: \"%s\"", etag);
- ret = http_send_header(etag_header);
- efree(etag_header);
-
- if (!etag_len){
- php_error_docref(NULL TSRMLS_CC,E_ERROR,
- "Sending empty Etag (previous: %s)\n", HTTP_G(etag));
- return FAILURE;
- }
- /* remember */
- if (HTTP_G(etag)) {
- efree(HTTP_G(etag));
- }
- HTTP_G(etag) = estrdup(etag);
-
- return ret;
-}
-/* }}} */
-
-/* {{{ char *http_absolute_uri(char *, char *) */
-PHP_HTTP_API char *_http_absolute_uri(const char *url,
- const char *proto TSRMLS_DC)
-{
- char URI[HTTP_URI_MAXLEN + 1], *PTR, *proto_ptr, *host, *path;
- zval *zhost;
-
- if (!url || !strlen(url)) {
- if (!SG(request_info).request_uri) {
- return NULL;
- }
- url = SG(request_info).request_uri;
- }
- /* Mess around with already absolute URIs */
- else if (proto_ptr = strstr(url, "://")) {
- if (!proto || !strncmp(url, proto, strlen(proto))) {
- return estrdup(url);
- } else {
- snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
- return estrdup(URI);
- }
- }
-
- /* protocol defaults to http */
- if (!proto || !strlen(proto)) {
- proto = "http";
- }
-
- /* get host name */
- if ( (zhost = http_get_server_var("HTTP_HOST")) ||
- (zhost = http_get_server_var("SERVER_NAME"))) {
- host = Z_STRVAL_P(zhost);
- } else {
- host = "localhost";
- }
-
-
- /* 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);
- }
-
- /* strip everything after a new line */
- PTR = URI;
- while (*PTR != 0) {
- if (*PTR == '\n' || *PTR == '\r') {
- *PTR = 0;
- break;
- }
- PTR++;
- }
-
- return estrdup(URI);
-}
-/* }}} */
-
-/* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
-PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported,
- const char *def TSRMLS_DC)
-{
- zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
- char *q_ptr, *result;
- int i, c;
- double qual;
-
- HTTP_GSC(zaccept, entry, estrdup(def));
-
- MAKE_STD_ZVAL(zarray);
- array_init(zarray);
-
- MAKE_STD_ZVAL(zdelim);
- ZVAL_STRING(zdelim, ",", 0);
- php_explode(zdelim, zaccept, zarray, -1);
- efree(zdelim);
-
- MAKE_STD_ZVAL(zentries);
- array_init(zentries);
-
- c = zend_hash_num_elements(Z_ARRVAL_P(zarray));
- for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) {
-
- if (SUCCESS != zend_hash_get_current_data(
- Z_ARRVAL_P(zarray), (void **) &zentry)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept));
- break;
- }
-
- /* check for qualifier */
- if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
- qual = strtod(q_ptr + 3, NULL);
- } else {
- qual = 1000.0 - i;
- }
-
- /* walk through the supported array */
- for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported));
- SUCCESS == zend_hash_get_current_data(
- Z_ARRVAL_P(supported), (void **) &zsupp);
- zend_hash_move_forward(Z_ARRVAL_P(supported))) {
- if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
- add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual);