You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by sf...@apache.org on 2011/08/26 01:02:43 UTC

svn commit: r1161791 - /httpd/httpd/trunk/modules/http/byterange_filter.c

Author: sf
Date: Thu Aug 25 23:02:43 2011
New Revision: 1161791

URL: http://svn.apache.org/viewvc?rev=1161791&view=rev
Log:
Put parsed ranges into an array and perform merges on that array.

Modified:
    httpd/httpd/trunk/modules/http/byterange_filter.c

Modified: httpd/httpd/trunk/modules/http/byterange_filter.c
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/http/byterange_filter.c?rev=1161791&r1=1161790&r2=1161791&view=diff
==============================================================================
--- httpd/httpd/trunk/modules/http/byterange_filter.c (original)
+++ httpd/httpd/trunk/modules/http/byterange_filter.c Thu Aug 25 23:02:43 2011
@@ -303,6 +303,10 @@ static apr_status_t copy_brigade_range(a
     return APR_SUCCESS;
 }
 
+struct range_spec {
+    apr_off_t start, end;
+};
+
 AP_CORE_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f,
                                                          apr_bucket_brigade *bb)
 {
@@ -313,13 +317,12 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_
     apr_bucket *e;
     apr_bucket_brigade *bsend;
     apr_bucket_brigade *tmpbb;
-    apr_off_t range_start;
-    apr_off_t range_end;
     char *current;
     apr_off_t clength = 0;
     apr_status_t rv;
     int found = 0;
-    int num_ranges;
+    int num_ranges, i = 0;
+    struct range_spec *ranges;
 
     /* Iterate through the brigade until reaching EOS or a bucket with
      * unknown length. */
@@ -348,11 +351,40 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_
     }
 
     ctx = apr_pcalloc(r->pool, sizeof(*ctx));
-    ctx->num_ranges = num_ranges;
     /* create a brigade in case we never call ap_save_brigade() */
     ctx->bb = apr_brigade_create(r->pool, c->bucket_alloc);
 
-    if (ctx->num_ranges > 1) {
+
+    /* this brigade holds what we will be sending */
+    bsend = apr_brigade_create(r->pool, c->bucket_alloc);
+    tmpbb = apr_brigade_create(r->pool, c->bucket_alloc);
+    ranges = apr_palloc(r->pool, sizeof(*ranges) * num_ranges);
+
+    while ((current = ap_getword(r->pool, &r->range, ','))
+           && (rv = parse_byterange(current, clength, &ranges[i].start,
+                                    &ranges[i].end))) {
+        if (rv == -1) {
+            continue;
+        }
+        else if (rv == 0)
+            break;
+
+        if (i > 0) {
+            if (!(ranges[i].start > ranges[i-1].end   + 1 &&
+                  ranges[i].end   < ranges[i-1].start - 1))
+            {
+                if (ranges[i].start < ranges[i-1].start)
+                    ranges[i-1].start = ranges[i].start;
+                if (ranges[i].end > ranges[i-1].end)
+                    ranges[i-1].end = ranges[i].end;
+                continue;
+            }
+        }
+        i++;
+    }
+    num_ranges = i;
+
+    if (num_ranges > 1) {
         /* Is ap_make_content_type required here? */
         const char *orig_ct = ap_make_content_type(r, r->content_type);
         ctx->boundary = apr_psprintf(r->pool, "%" APR_UINT64_T_HEX_FMT "%lx",
@@ -381,24 +413,14 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_
         ap_xlate_proto_to_ascii(ctx->bound_head, strlen(ctx->bound_head));
     }
 
-    /* this brigade holds what we will be sending */
-    bsend = apr_brigade_create(r->pool, c->bucket_alloc);
-    tmpbb = apr_brigade_create(r->pool, c->bucket_alloc);
-
-    while ((current = ap_getword(r->pool, &r->range, ','))
-           && (rv = parse_byterange(current, clength, &range_start,
-                                    &range_end))) {
-        if (rv == -1) {
-            continue;
-        }
-
-        rv = copy_brigade_range(bb, tmpbb, range_start, range_end);
+    for (i = 0; i < num_ranges; i++) {
+        rv = copy_brigade_range(bb, tmpbb, ranges[i].start, ranges[i].end);
         if (rv != APR_SUCCESS ) {
             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
                           "brigade_copy_range() failed " "[%" APR_OFF_T_FMT
                           "-%" APR_OFF_T_FMT ",%"
                           APR_OFF_T_FMT "]",
-                          range_start, range_end, clength);
+                          ranges[i].start, ranges[i].end, clength);
             continue;
         }
         found = 1;
@@ -406,10 +428,10 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_
         /* For single range requests, we must produce Content-Range header.
          * Otherwise, we need to produce the multipart boundaries.
          */
-        if (ctx->num_ranges == 1) {
+        if (num_ranges == 1) {
             apr_table_setn(r->headers_out, "Content-Range",
                            apr_psprintf(r->pool, "bytes " BYTERANGE_FMT,
-                                        range_start, range_end, clength));
+                                        ranges[i].start, ranges[i].end, clength));
         }
         else {
             char *ts;
@@ -419,7 +441,7 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_
             APR_BRIGADE_INSERT_TAIL(bsend, e);
 
             ts = apr_psprintf(r->pool, BYTERANGE_FMT CRLF CRLF,
-                              range_start, range_end, clength);
+                              ranges[i].start, ranges[i].end, clength);
             ap_xlate_proto_to_ascii(ts, strlen(ts));
             e = apr_bucket_pool_create(ts, strlen(ts), r->pool,
                                        c->bucket_alloc);
@@ -441,7 +463,7 @@ AP_CORE_DECLARE_NONSTD(apr_status_t) ap_
         return ap_pass_brigade(f->next, bsend);
     }
 
-    if (ctx->num_ranges > 1) {
+    if (num_ranges > 1) {
         char *end;
 
         /* add the final boundary */



Re: svn commit: r1161791 - /httpd/httpd/trunk/modules/http/byterange_filter.c

Posted by Greg Ames <am...@gmail.com>.
doesn't matter, it's not there in the latest.

On Fri, Aug 26, 2011 at 11:02 AM, "Plüm, Rüdiger, VF-Group" <
ruediger.pluem@vodafone.com> wrote:

> **
> I guess this should catch cases where you have overlapping or adjacent
> ranges next to each other but in the wrong order,
> so e.g.
>
> 2000-3000,1000-2000
>
> or
>
> 2000-3000,1500-2500
>
> But I think this check is currently wrong as you point out. Currently not
> sure how to fix it.
>
> Regards
>
> Rüdiger
>
>  ------------------------------
> *From:* Greg Ames [mailto:ames.greg@gmail.com]
> *Sent:* Freitag, 26. August 2011 16:58
> *To:* dev@httpd.apache.org
> *Subject:* Re: svn commit: r1161791 -
> /httpd/httpd/trunk/modules/http/byterange_filter.c
>
>
>
> On Thu, Aug 25, 2011 at 7:02 PM, <sf...@apache.org> wrote:
>
>>
>> +            if (!(ranges[i].start > ranges[i-1].end   + 1 &&
>> +                  ranges[i].end   < ranges[i-1].start - 1))
>>
>> and the if condition looks similar.  the first test is fine, but why is
> ranges[i].end < ranges[i-1].start a good thing?  since we have the ! in
> front, we are testing for the normal ascending case.
>
> Greg
>
>

RE: svn commit: r1161791 - /httpd/httpd/trunk/modules/http/byterange_filter.c

Posted by "Plüm, Rüdiger, VF-Group" <ru...@vodafone.com>.
I guess this should catch cases where you have overlapping or adjacent ranges next to each other but in the wrong order,
so e.g.
 
2000-3000,1000-2000
 
or
 
2000-3000,1500-2500
 
But I think this check is currently wrong as you point out. Currently not sure how to fix it.
 
Regards
 
Rüdiger


________________________________

	From: Greg Ames [mailto:ames.greg@gmail.com] 
	Sent: Freitag, 26. August 2011 16:58
	To: dev@httpd.apache.org
	Subject: Re: svn commit: r1161791 - /httpd/httpd/trunk/modules/http/byterange_filter.c
	
	


	On Thu, Aug 25, 2011 at 7:02 PM, <sf...@apache.org> wrote:
	


		+            if (!(ranges[i].start > ranges[i-1].end   + 1 &&
		+                  ranges[i].end   < ranges[i-1].start - 1))
		
		

	and the if condition looks similar.  the first test is fine, but why is ranges[i].end < ranges[i-1].start a good thing?  since we have the ! in front, we are testing for the normal ascending case.
	
	Greg 
	



Re: svn commit: r1161791 - /httpd/httpd/trunk/modules/http/byterange_filter.c

Posted by Greg Ames <am...@gmail.com>.
On Thu, Aug 25, 2011 at 7:02 PM, <sf...@apache.org> wrote:

>
> +            if (!(ranges[i].start > ranges[i-1].end   + 1 &&
> +                  ranges[i].end   < ranges[i-1].start - 1))
>
> and the if condition looks similar.  the first test is fine, but why is
ranges[i].end < ranges[i-1].start a good thing?  since we have the ! in
front, we are testing for the normal ascending case.

Greg

Re: svn commit: r1161791 - /httpd/httpd/trunk/modules/http/byterange_filter.c

Posted by Stefan Fritsch <sf...@sfritsch.de>.
On Fri, August 26, 2011 16:47, Greg Ames wrote:
> On Thu, Aug 25, 2011 at 7:02 PM, <sf...@apache.org> wrote:
>
>>
>> Log:
>> Put parsed ranges into an array and perform merges on that array.
>>
>> +        if (i > 0) {
>> +            if (!(ranges[i].start > ranges[i-1].end   + 1 &&
>> +                  ranges[i].end   < ranges[i-1].start - 1))
>> +            {

The if above is meant to determine if we want to merge at all. But I think
you are right in that this is wrong. The "&&" should be an "||". But this
also means that the test framework needs more tests for multiple ranges.

>> +                if (ranges[i].start < ranges[i-1].start)
>> +                    ranges[i-1].start = ranges[i].start;
>>
>
> ok, the two lines above seem reasonable.
>
>
>> +                if (ranges[i].end > ranges[i-1].end)
>> +                    ranges[i-1].end = ranges[i].end;

These two ifs just merge the range. The first selects the lower of the two
start values and the second one selects the higher of the two end values.
This could be made more clear by defining MIN and MAX macros and writing

    ranges[i-1].start = MIN(ranges[i].start, ranges[i-1].start);
    ranges[i-1].end   = MAX(ranges[i].end,   ranges[i-1].end);

Does this make sense?


Re: svn commit: r1161791 - /httpd/httpd/trunk/modules/http/byterange_filter.c

Posted by Greg Ames <am...@gmail.com>.
On Thu, Aug 25, 2011 at 7:02 PM, <sf...@apache.org> wrote:

>
> Log:
> Put parsed ranges into an array and perform merges on that array.
>
> +        if (i > 0) {
> +            if (!(ranges[i].start > ranges[i-1].end   + 1 &&
> +                  ranges[i].end   < ranges[i-1].start - 1))
> +            {
> +                if (ranges[i].start < ranges[i-1].start)
> +                    ranges[i-1].start = ranges[i].start;
>

ok, the two lines above seem reasonable.


> +                if (ranges[i].end > ranges[i-1].end)
> +                    ranges[i-1].end = ranges[i].end;
>

I don't understand the two lines above.  Suppose we have
1000-2000,3000-4000.  That should pass thru unchanged.  But I believe we
will see 1000-4000,3000-4000 instead.

Greg