-/* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
-PHP_HTTP_API STATUS _http_send_ranges(HashTable *ranges, const void *data, size_t size, http_send_mode mode TSRMLS_DC)
-{
- zval **zbegin, **zend, **zrange;
-
- /* single range */
- if (zend_hash_num_elements(ranges) == 1) {
- char range_header[256] = {0};
-
- if (SUCCESS != zend_hash_index_find(ranges, 0, (void **) &zrange) ||
- SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &zbegin) ||
- SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &zend)) {
- http_send_status(500);
- return FAILURE;
- }
-
- /* Send HTTP 206 Partial Content */
- http_send_status(206);
-
- /* send content range header */
- snprintf(range_header, 255, "Content-Range: bytes %ld-%ld/%lu", Z_LVAL_PP(zbegin), Z_LVAL_PP(zend), (ulong) size);
- http_send_header_string(range_header);
-
- /* send requested chunk */
- return http_send_chunk(data, Z_LVAL_PP(zbegin), Z_LVAL_PP(zend) + 1, mode);
- }
-
- /* multi range */
- else {
- size_t preface_len;
- 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, "--%lu%0.9f", (ulong) time(NULL), php_combined_lcg(TSRMLS_C));
- strncat(multi_header, bound + 2, 21);
- http_send_header_string(multi_header);
-
- /* send each requested chunk */
- FOREACH_HASH_VAL(ranges, zrange) {
- if (SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &zbegin) ||
- SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &zend)) {
- break;
- }
-
- preface_len = snprintf(preface, 1023,
- HTTP_CRLF "%s"
- HTTP_CRLF "Content-Type: %s"
- HTTP_CRLF "Content-Range: bytes %ld-%ld/%lu"
- HTTP_CRLF
- HTTP_CRLF,
-
- bound,
- HTTP_G(send).content_type ? HTTP_G(send).content_type : "application/x-octetstream",
- Z_LVAL_PP(zbegin),
- Z_LVAL_PP(zend),
- (ulong) size
- );
-
- PHPWRITE(preface, preface_len);
- http_send_chunk(data, Z_LVAL_PP(zbegin), Z_LVAL_PP(zend) + 1, mode);
- }
-
- /* write boundary once more */
- PHPWRITE(HTTP_CRLF, lenof(HTTP_CRLF));
- PHPWRITE(bound, strlen(bound));
- PHPWRITE("--", lenof("--"));
-
- return SUCCESS;
- }
-}
-/* }}} */
-