- qual = 1000.0 - 1;
- }
-
- /* 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);
- break;
- }
- }
- }
-
- zval_dtor(zarray);
- efree(zarray);
-
- zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
-
- if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort,
- http_sort_q, 0 TSRMLS_CC)) ||
- (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key(
- Z_ARRVAL_P(zentries), &result, 0, 1))) {
- result = estrdup(def);
- }
-
- zval_dtor(zentries);
- efree(zentries);
-
- return result;
-}
-/* }}} */
-
-/* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
-PHP_HTTP_API http_range_status _http_get_request_ranges(zval *zranges,
- const size_t length TSRMLS_DC)
-{
- zval *zrange, *zentry, **zentries;
- char *range, c;
- long begin = -1, end = -1, *ptr;
-
- HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
- range = Z_STRVAL_P(zrange);
-
- if (strncmp(range, "bytes=", strlen("bytes="))) {
- php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Range header misses bytes=");
- return RANGE_ERR;
- }
-
- ptr = &begin;
- range += strlen("bytes=");
-
- do {
- switch (c = *(range++))
- {
- case '0':
- *ptr *= 10;
- break;
-
- case '1': case '2': case '3':
- case '4': case '5': case '6':
- case '7': case '8': case '9':
- /*
- * If the value of the pointer is already set (non-negative)
- * then multiply its value by ten and add the current value,
- * else initialise the pointers value with the current value
- * --
- * This let us recognize empty fields when validating the
- * ranges, i.e. a "-10" for begin and "12345" for the end
- * was the following range request: "Range: bytes=0-12345";
- * While a "-1" for begin and "12345" for the end would
- * have been: "Range: bytes=-12345".
- */
- if (*ptr > 0) {
- *ptr *= 10;
- *ptr += c - '0';
- } else {
- *ptr = c - '0';
- }
- break;
-
- case '-':
- ptr = &end;
- break;
-
- case 0:
- case ',':
-
- if (length) {
- /* validate ranges */
- switch (begin)
- {
- /* "0-12345" */
- case -10:
- if ((length - end) < 1) {
- return RANGE_ERR;
- }
- begin = 0;
- break;
-
- /* "-12345" */
- case -1:
- if ((length - end) < 1) {
- return RANGE_ERR;
- }
- begin = length - end;
- end = length;
- break;
-
- /* "12345-(xxx)" */
- default:
- switch (end)
- {
- /* "12345-" */
- case -1:
- if ((length - begin) < 1) {
- return RANGE_ERR;
- }
- end = length - 1;
- break;
-
- /* "12345-67890" */
- default:
- if ( ((length - begin) < 1) ||
- ((length - end) < 1) ||
- ((begin - end) >= 0)) {
- return RANGE_ERR;
- }
- break;
- }
- break;
- }
- }
-
- zentry = (zval *) zentries++;
- MAKE_STD_ZVAL(zentry);
- array_init(zentry);
- add_index_long(zentry, 0, begin);
- add_index_long(zentry, 1, end);
- add_next_index_zval(zranges, zentry);
-
- begin = -1;
- end = -1;
- ptr = &begin;
- break;
-
- default:
- return RANGE_ERR;
- break;
- }
- } while (c != 0);
-
- return RANGE_OK;
-}
-/* }}} */
-
-/* {{{ 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)
-{
- zval **zrange;
- long b, e, **begin, **end;
- char range_header[255], multi_header[68] = "Content-Type: multipart/byteranges; boundary=", bound[23], preface[1024];
- int i, c;
-
- /* Send HTTP 206 Partial Content */
- http_send_status(206);
-
- /* single range */
- if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
- 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);
-
- /* so zranges can be freed */
- b = **begin;
- e = **end;
-
- /* free zranges */
- zval_dtor(zranges);
- efree(zranges);
-
- /* send content range header */
- snprintf(range_header, 255, "Content-Range: bytes %d-%d/%d", b, e, size);
- http_send_header(range_header);
-
- /* send requested chunk */
- return http_send_chunk(data, b, e + 1, mode);
- }
-
- /* multi range */
-
- snprintf(bound, 23, "--%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)) {
- break;
- }
-
- snprintf(preface, 1024,
- "\r\n%s\r\nContent-Type: %s\r\nContent-Range: bytes %d-%d/%d\r\n\r\n", bound,
- HTTP_G(ctype) ? HTTP_G(ctype) : "application/x-octetstream",
- **begin, **end, size);
- php_body_write(preface, strlen(preface) TSRMLS_CC);
- http_send_chunk(data, **begin, **end + 1, mode);
-
- }
-
- /* free zranges */
- zval_dtor(zranges);
- efree(zranges);
-
- /* write boundary once more */
- php_body_write("\n", 1 TSRMLS_CC);
- php_body_write(bound, strlen(bound) TSRMLS_CC);
-
- return SUCCESS;
-}
-/* }}} */
-
-/* {{{ STATUS http_send(void *, sizezo_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)
-{
- zval *zranges = NULL;
-
- if (!data_ptr) {
- return FAILURE;
- }
-
- /* never ever use the output to compute the ETag if http_send() is used */
- if (HTTP_G(etag_started)) {
- char *new_etag = (char *) emalloc(33);
-
- php_end_ob_buffer(0, 0 TSRMLS_CC);
- if (NULL == http_etag(&new_etag, data_ptr, data_size, data_mode)) {
- efree(new_etag);
- return FAILURE;
- }
-
- /* send 304 Not Modified if etag matches */
- if (http_etag_match("HTTP_IF_NONE_MATCH", new_etag)) {
- efree(new_etag);
- return http_send_status(304);
- }
-
- http_send_etag(new_etag, 32);
- efree(new_etag);
- }
-
- if (http_is_range_request()) {
-
- MAKE_STD_ZVAL(zranges);
- array_init(zranges);
-
- switch (http_get_request_ranges(zranges, data_size))
- {
- case RANGE_NO:
- zval_dtor(zranges);
- efree(zranges);
- /* go ahead and send all */
- break;
-
- case RANGE_OK:
- return http_send_ranges(zranges, data_ptr, data_size, data_mode);
- break;
-
- case RANGE_ERR:
- zval_dtor(zranges);
- efree(zranges);
- http_send_status(416);
- return FAILURE;
- break;
-
- default:
- return FAILURE;
- break;
- }
- }
- /* send all */
- return http_send_chunk(data_ptr, 0, data_size, data_mode);
-}
-/* }}} */
-
-/* {{{ 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((void *) 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((void *) 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_stream *file;
- STATUS ret;
-
- if (!Z_STRLEN_P(zfile)) {
- return FAILURE;
- }
-
- if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
- REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
- return FAILURE;
- }
-
- ret = http_send_stream(file);
- php_stream_close(file);
- return ret;
-}
-/* }}} */
-
-/* {{{ proto 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)
-{
- const char *e_ptr;
- char *d_ptr;
-
- *decoded_len = 0;
- *decoded = (char *) ecalloc(1, encoded_len);
- d_ptr = *decoded;
- e_ptr = encoded;
-
- while (((e_ptr - encoded) - encoded_len) > 0) {
- char hex_len[9] = {0};
- size_t chunk_len = 0;
- int i = 0;
-
- /* read in chunk size */
- while (isxdigit(*e_ptr)) {
- if (i == 9) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "Chunk size is too long: 0x%s...", hex_len);
- efree(*decoded);
- return FAILURE;
- }
- hex_len[i++] = *e_ptr++;