You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@perl.apache.org by Stas Bekman <st...@stason.org> on 2003/05/15 10:04:24 UTC

[patch] C implementation of $r->content + rfc on the name

recently I've learn that the implementation of ap_get_client_block is buggy. 
For example it can't handle situations where EOS arrives in the bucket brigade 
as data. There was a short followup on the httpd-dev list, which has quickly 
died. But the fact remains, we can't rely on using ap_get_client_block even in 
Apache::compat. I've already rewritten ModPerl::Test::read_post to read the 
POST data using bbs and it works fine. I was just about to replace the 
$r->content implementation in Apache::compat when I've realized that it's too 
slow and should be done in C.

Notice that this C implementation is the same as $r->content() in the scalar 
context in mp1, though it handles chunked encoding and doesn't rely on C-L 
header. It simply delegates these headaches to httpd. This C implementation 
just reads the POST data, just like $r->args returns just the QUERY_STRING. It 
won't handle any multi-part POST. Though I think it might be useful and faster 
then any other fully-fledged implementation, when all you want is to grab the 
POSTed data as is.

Let me know if you have any problems with that. The only part is I hate is 
that since we have to keep the same name in Apache/compat.pm, once someone 
loads Apache::compat, $r->content is going to be overloaded to handle list 
context as well. May be we should give this new mp2 function a new name? e.g. 
$r->body?


Index: lib/Apache/compat.pm
===================================================================
RCS file: /home/cvs/modperl-2.0/lib/Apache/compat.pm,v
retrieving revision 1.85
diff -u -r1.85 compat.pm
--- lib/Apache/compat.pm	11 Apr 2003 07:34:03 -0000	1.85
+++ lib/Apache/compat.pm	15 May 2003 07:52:48 -0000
@@ -285,24 +285,15 @@

  use constant IOBUFSIZE => 8192;

-sub content {
-    my $r = shift;
+{
+    my $content_sub = *Apache::RequestRec::content{CODE};
+    *Apache::RequestRec::content = sub {
+        my $r = shift;

-    $r->setup_client_block;
-
-    return undef unless $r->should_client_block;
-
-    my $data = '';
-    my $buf;
-    while (my $read_len = $r->get_client_block($buf, IOBUFSIZE)) {
-        if ($read_len == -1) {
-            die "some error while reading with get_client_block";
-        }
-        $data .= $buf;
+        my $data = $r->$content_sub;
+        return $data unless wantarray;
+        return $r->parse_args($data);
      }
-
-    return $data unless wantarray;
-    return $r->parse_args($data);
  }

  sub clear_rgy_endav {
Index: xs/Apache/RequestIO/Apache__RequestIO.h
===================================================================
RCS file: /home/cvs/modperl-2.0/xs/Apache/RequestIO/Apache__RequestIO.h,v
retrieving revision 1.37
diff -u -r1.37 Apache__RequestIO.h
--- xs/Apache/RequestIO/Apache__RequestIO.h	14 Mar 2003 05:33:19 -0000	1.37
+++ xs/Apache/RequestIO/Apache__RequestIO.h	15 May 2003 07:52:48 -0000
@@ -245,6 +245,60 @@
      return total;
  }

+
+/* get the POSTed body as-is */
+static SV *mpxs_Apache__RequestRec_content(pTHX_ request_rec *r)
+{
+    SV *sv = newSVpvn("", 0);
+    int rc;
+    int seen_eos = 0;
+    apr_bucket_brigade *bb = apr_brigade_create(r->pool,
+                                                r->connection->bucket_alloc);
+
+    do {
+        char *buffer;
+        apr_bucket *b;
+        apr_size_t bufsiz = HUGE_STRING_LEN;
+
+        rc = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
+                            APR_BLOCK_READ, HUGE_STRING_LEN);
+        if (rc != APR_SUCCESS) {
+            apr_brigade_destroy(bb);
+            Perl_croak(aTHX_ modperl_apr_strerror(rc));
+        }
+
+        /* If this fails, it means that a filter is written
+         * incorrectly and that it needs to learn how to properly
+         * handle APR_BLOCK_READ requests by returning data when
+         * requested.
+         */
+        AP_DEBUG_ASSERT(!APR_BRIGADE_EMPTY(bb));
+
+        /* search for EOS */
+        APR_BRIGADE_FOREACH(b, bb) {
+            if (APR_BUCKET_IS_EOS(b)) {
+                seen_eos = 1;
+                break;
+            }
+        }
+
+        rc = apr_brigade_pflatten(bb, &buffer, &bufsiz, r->pool);
+        if (rc != APR_SUCCESS) {
+            apr_brigade_destroy(bb);
+            Perl_croak(aTHX_ modperl_apr_strerror(rc));
+        }
+
+        // XXX: more efficient way?
+        sv_catpvn(sv, buffer, bufsiz);
+        apr_brigade_cleanup(bb);
+    }
+    while (!seen_eos);
+
+    apr_brigade_destroy(bb);
+
+    return sv;
+}
+
  static MP_INLINE
  SV *mpxs_Apache__RequestRec_GETC(pTHX_ request_rec *r)
  {
Index: xs/maps/modperl_functions.map
===================================================================
RCS file: /home/cvs/modperl-2.0/xs/maps/modperl_functions.map,v
retrieving revision 1.56
diff -u -r1.56 modperl_functions.map
--- xs/maps/modperl_functions.map	1 Apr 2003 05:20:50 -0000	1.56
+++ xs/maps/modperl_functions.map	15 May 2003 07:52:48 -0000
@@ -45,6 +45,7 @@
   SV *:DEFINE_UNTIE    | | request_rec *:r, int:refcnt
   mpxs_Apache__RequestRec_sendfile | | r, filename=r->filename, offset=0, len=0
   mpxs_Apache__RequestRec_read | | r, buffer, bufsiz, offset=0
+ mpxs_Apache__RequestRec_content | | r
   long:DEFINE_READ | | request_rec *:r, SV *:buffer, int:bufsiz, int:offset=0
   mpxs_Apache__RequestRec_write | | r, buffer, bufsiz=-1, offset=0
   mpxs_Apache__RequestRec_print | | ...
Index: xs/tables/current/ModPerl/FunctionTable.pm
===================================================================
RCS file: /home/cvs/modperl-2.0/xs/tables/current/ModPerl/FunctionTable.pm,v
retrieving revision 1.114
diff -u -r1.114 FunctionTable.pm
--- xs/tables/current/ModPerl/FunctionTable.pm	12 May 2003 13:00:15 -0000	1.114
+++ xs/tables/current/ModPerl/FunctionTable.pm	15 May 2003 07:52:48 -0000
@@ -5595,6 +5595,23 @@
      ]
    },
    {
+    'return_type' => 'SV *',
+    'name' => 'mpxs_Apache__RequestRec_content',
+    'attr' => [
+      'static'
+    ],
+    'args' => [
+      {
+        'type' => 'PerlInterpreter *',
+        'name' => 'my_perl'
+      },
+      {
+        'type' => 'request_rec *',
+        'name' => 'r'
+      },
+    ]
+  },
+  {
      'return_type' => 'int',
      'name' => 'mpxs_Apache__RequestRec_OPEN',
      'args' => [

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Joe Schaefer wrote:
> Stas Bekman <st...@stason.org> writes:
> 
> [...]
> 
> 
>>Correct. But:
>>
>>my $req = Apache::Request->new($r);
>>
>>is neither SetOutputFilter and nor
>>$r->add_output_filter(\&apreqfilter); in the 
>>user's code.
>>
>>my point is that it's far from obvious that Apache::Request->new() is
>>really:  $r->add_output_filter(\&apreqfilter); Perhaps Apache::Request
>>should make Apache::Request::filter available to users, so that they
>>can configure it manually (optionally of course). new() somehow should
>>be aware of that. 
> 
> 
> s/output/input/g, of course.  So we'll probably need to 
> XS-ify the apreq filter also, so mod_perl users who want 
> control over it's behavior can have it.

Correct.

>>will mod_apreq always be available to those who build libapreq-2?
> 
> 
> I'm not sure how to answer that.  To use Apache::Request within 
> apache-2, people will need to install and load the filter module
> into their httpd.conf:
> 
>   LoadModule apreq_module       modules/mod_apreq.so
> 
> Sorry, I see no way around that other than having the C library
> integrated into the httpd core.  Since that seems impossible now,
> apreq users will have to suffer through this apxs business, 
> especially if their httpd is without DSO support. In that
> case, Apache::Request will require a full recompile of httpd.

Of course.

As you suggested this API will be polished once it'll be used.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Joe Schaefer <jo...@sunstarsys.com>.
Stas Bekman <st...@stason.org> writes:

[...]

> Correct. But:
> 
> my $req = Apache::Request->new($r);
> 
> is neither SetOutputFilter and nor
> $r->add_output_filter(\&apreqfilter); in the 
> user's code.
> 
> my point is that it's far from obvious that Apache::Request->new() is
> really:  $r->add_output_filter(\&apreqfilter); Perhaps Apache::Request
> should make Apache::Request::filter available to users, so that they
> can configure it manually (optionally of course). new() somehow should
> be aware of that. 

s/output/input/g, of course.  So we'll probably need to 
XS-ify the apreq filter also, so mod_perl users who want 
control over it's behavior can have it.

[...]

> will mod_apreq always be available to those who build libapreq-2?

I'm not sure how to answer that.  To use Apache::Request within 
apache-2, people will need to install and load the filter module
into their httpd.conf:

  LoadModule apreq_module       modules/mod_apreq.so

Sorry, I see no way around that other than having the C library
integrated into the httpd core.  Since that seems impossible now,
apreq users will have to suffer through this apxs business, 
especially if their httpd is without DSO support. In that
case, Apache::Request will require a full recompile of httpd.

-- 
Joe Schaefer


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Joe Schaefer wrote:
> Stas Bekman <st...@stason.org> writes:
> 
> [...]
> 
> 
>>I haven't looked at the AP_MODE_SPECULATIVE yet :( I will.
>>
>>My main concern that the above doesn't answer is how the apreq filter
>>co-operates with other custom input filters. It has to be inserted on
>>the very top, so not to miss any transformations on the request body,
>>but Apache provides no API to ensure that a certain filter will be run
>>very last (which is actually very first in the input-terminology).
> 
> 
> It's a catch 22 at the moment.  How does ANY request filter that 
> modifies to incoming buckets ensure that it's added to the input filter 
> chain at just the right spot? 

By giving the control to users. They decide when each filter is inserted.

> Correct me if I'm wrong, but it seems to 
> me that ordering the input chain is something a user has to take
> considerable care to ensure.

Absolutely.

> Up to now, I've been working under the assumption that
> 
>   1) automatic filter injection for apreq will be
>      Apache::Request::new()'s responsibility.  If 
>      any handler wants to use Apache::Request, the 
>      apreq filter will push itself onto r->filters_in
>      at this line
> 
>         my $req = Apache::Request->new($r);
> 
>      If a filter wants apreq to see its transformations,
>      it had better already be in the filter chain before
>      then.

Correct. But:

my $req = Apache::Request->new($r);

is neither SetOutputFilter and nor $r->add_output_filter(\&apreqfilter); in 
the user's code.

my point is that it's far from obvious that Apache::Request->new() is really:
$r->add_output_filter(\&apreqfilter); Perhaps Apache::Request should make 
Apache::Request::filter available to users, so that they can configure it 
manually (optionally of course). new() somehow should be aware of that.

>   2) the mod_apreq filter can also be manually injected by 
>      the user, by using the standard apache filter api, 
>      in cases where apreq might be otherwise inject itself 
>      prematurely.

will mod_apreq always be available to those who build libapreq-2?

>>I suppose that this somehow will work if apreq filter injects itself
>>at the very last moment and the user has to be aware of how it works,
> 
> 
> Sure, and so will the developers :-).  I'm sure we'll
> have to make lots of modifications to the filter code
> once the perl API is available for testing.  We'll
> probably also need help from httpd on how 
> AP_MODE_SPECULATIVE is *supposed* to be implemented 
> by transforming filters, since the typical bow-out 
> behavior a'la mod_deflate is surely incorrect.

There are too many vague things in 2.0 I haven't had a chance to play with, so 
I can't even inteligently comment on it :(

 From my experience of writing lots of filters for 2.0, hardly anybody writes 
custom filters (other than mod_perl) and since the core filters seem to work, 
nobody at the httpd land cares much about polishing the filter API.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Joe Schaefer <jo...@sunstarsys.com>.
Stas Bekman <st...@stason.org> writes:

[...]

> I haven't looked at the AP_MODE_SPECULATIVE yet :( I will.
> 
> My main concern that the above doesn't answer is how the apreq filter
> co-operates with other custom input filters. It has to be inserted on
> the very top, so not to miss any transformations on the request body,
> but Apache provides no API to ensure that a certain filter will be run
> very last (which is actually very first in the input-terminology).

It's a catch 22 at the moment.  How does ANY request filter that 
modifies to incoming buckets ensure that it's added to the input filter 
chain at just the right spot?  Correct me if I'm wrong, but it seems to 
me that ordering the input chain is something a user has to take
considerable care to ensure.

Up to now, I've been working under the assumption that

  1) automatic filter injection for apreq will be
     Apache::Request::new()'s responsibility.  If 
     any handler wants to use Apache::Request, the 
     apreq filter will push itself onto r->filters_in
     at this line

        my $req = Apache::Request->new($r);

     If a filter wants apreq to see its transformations,
     it had better already be in the filter chain before
     then.


  2) the mod_apreq filter can also be manually injected by 
     the user, by using the standard apache filter api, 
     in cases where apreq might be otherwise inject itself 
     prematurely.

> I suppose that this somehow will work if apreq filter injects itself
> at the very last moment and the user has to be aware of how it works,

Sure, and so will the developers :-).  I'm sure we'll
have to make lots of modifications to the filter code
once the perl API is available for testing.  We'll
probably also need help from httpd on how 
AP_MODE_SPECULATIVE is *supposed* to be implemented 
by transforming filters, since the typical bow-out 
behavior a'la mod_deflate is surely incorrect.

-- 
Joe Schaefer


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Joe Schaefer wrote:
> Stas Bekman <st...@stason.org> writes:
> 
> [...]
> 
> 
>>This is going to be a problem, btw. How users are going to trigger
>>that "tee"? 
> 
> 
> By calling Apache::Request::new().  The filter is passive-aggressive,
> which means it pulls nothing in all by itself.  It just passively
> copies buckets in the input brigade and hands those buckets off to
> the parser.
> 
> The filter only prefetches if someone calls Apache::Request::param, 
> and the underlying apreq_param() function cannot find the key among 
> the already-parsed data.  In that case, it prefetches data via 
> AP_MODE_SPECULATIVE mode, parses what it gets, and then later skips
> the prefetched data during normal AP_MODE_READ operation.
> 
> 
>>If someone needs to read the POST data in the pre-response handler,
>>can they do it? What if response handler injects a new input filter?
>>How do you ensure that apreq is injected on the very top?
> 
> 
> With code, if we really require that :-).  I don't think we do though,
> just look at the AP_MODE_SPECULATIVE support in httpd's core input filter.
> 
> 
>>I think that by default apreq should work as before, simply sucking
>>the POST data on demand. 
> 
> 
> It still works just as before. The only difference is that with
> the filter, we do *less* not more.  In apreq-1, a call to
> Apache::Request::param induces a full parse of the POST data.
> We no longer do that- we simply can't.  It amounts to the same 
> complaint I have with $r->content as a request note.
> 
> 
>>Only if a user configures it as an input filter, they have to have an
>>explicit control over it, i.e. they have to use the mod_perl API to
>>insert that filter explicitly.
> 
> 
> No- the filter just goes in as needed, and it just plain-old works.
> The end (perl) user should not care one iota about how apreq is
> implemented.

I haven't looked at the AP_MODE_SPECULATIVE yet :( I will.

My main concern that the above doesn't answer is how the apreq filter 
co-operates with other custom input filters. It has to be inserted on the very 
top, so not to miss any transformations on the request body, but Apache 
provides no API to ensure that a certain filter will be run very last (which 
is actually very first in the input-terminology).

I suppose that this somehow will work if apreq filter injects itself at the 
very last moment and the user has to be aware of how it works, so there would 
be no questions of why their POST data didn't go through their filter if they 
have inserted their custom filter after calling $apreq->param(foo) which has 
triggered POST read.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Joe Schaefer <jo...@sunstarsys.com>.
Stas Bekman <st...@stason.org> writes:

[...]

> This is going to be a problem, btw. How users are going to trigger
> that "tee"? 

By calling Apache::Request::new().  The filter is passive-aggressive,
which means it pulls nothing in all by itself.  It just passively
copies buckets in the input brigade and hands those buckets off to
the parser.

The filter only prefetches if someone calls Apache::Request::param, 
and the underlying apreq_param() function cannot find the key among 
the already-parsed data.  In that case, it prefetches data via 
AP_MODE_SPECULATIVE mode, parses what it gets, and then later skips
the prefetched data during normal AP_MODE_READ operation.

> If someone needs to read the POST data in the pre-response handler,
> can they do it? What if response handler injects a new input filter?
> How do you ensure that apreq is injected on the very top?

With code, if we really require that :-).  I don't think we do though,
just look at the AP_MODE_SPECULATIVE support in httpd's core input filter.

> I think that by default apreq should work as before, simply sucking
> the POST data on demand. 

It still works just as before. The only difference is that with
the filter, we do *less* not more.  In apreq-1, a call to
Apache::Request::param induces a full parse of the POST data.
We no longer do that- we simply can't.  It amounts to the same 
complaint I have with $r->content as a request note.

> Only if a user configures it as an input filter, they have to have an
> explicit control over it, i.e. they have to use the mod_perl API to
> insert that filter explicitly.

No- the filter just goes in as needed, and it just plain-old works.
The end (perl) user should not care one iota about how apreq is
implemented.

-- 
Joe Schaefer

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Joe Schaefer wrote:

>>Well, then what is the better solution if 2 different modules need
>>access to the POSTed data (I didn't look at how apreq handles this) ?
> 
> 
> In (the C API for) apreq-2, we inject a filter into the input chain.
> It's essentially a passive "tee":
> 
> 
> incoming POST data =>  [ap_http_filter] => ... 
> 
>                      ... => [apreq filter] => ... [content handler]
>                                  |
>                                  |
>                                  v
>                               [parser] -> [hook] -> apr_table

> The filter is only injected once per-request, and the POST data should
> available to any phase of the request.  For example, suppose an aaa 
> handler needs apreq-2 to look for something in the POST data.  The 
> apreq filter will first prefetch some (configurable) amount of the 
> POST data, and then
> 
>   1) store the raw POST data in an internal brigade,
>      so the actual content handler will be able to get it later on,
> 
>   2) send a copy of whatever it got to the parser, to honor
>      the aaa handlers' request.

This is going to be a problem, btw. How users are going to trigger that "tee"? 
If someone needs to read the POST data in the pre-response handler, can they 
do it? What if response handler injects a new input filter? How do you ensure 
that apreq is injected on the very top?

I think that by default apreq should work as before, simply sucking the POST 
data on demand. Only if a user configures it as an input filter, they have to 
have an explicit control over it, i.e. they have to use the mod_perl API to 
insert that filter explicitly.


-- 


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Joe Schaefer <jo...@sunstarsys.com>.
"Philippe M. Chiasson" <go...@cpan.org> writes:

[...]

> Well, then what is the better solution if 2 different modules need
> access to the POSTed data (I didn't look at how apreq handles this) ?

In (the C API for) apreq-2, we inject a filter into the input chain.
It's essentially a passive "tee":


incoming POST data =>  [ap_http_filter] => ... 

                     ... => [apreq filter] => ... [content handler]
                                 |
                                 |
                                 v
                              [parser] -> [hook] -> apr_table
                                 
The filter is only injected once per-request, and the POST data should
available to any phase of the request.  For example, suppose an aaa 
handler needs apreq-2 to look for something in the POST data.  The 
apreq filter will first prefetch some (configurable) amount of the 
POST data, and then

  1) store the raw POST data in an internal brigade,
     so the actual content handler will be able to get it later on,

  2) send a copy of whatever it got to the parser, to honor
     the aaa handlers' request.

If apreq doesn't find what the aaa handler needs, the aaa handler can
repeat the process if it really wants to.  But it probably would be
better off to just reject (or decline) the request in such a case.

The current implementation of the apreq-2 filter isn't exactly problem
free at the moment (e.g. internal redirects are still messy), but I'm sure 
this is the right approach.

> I would many times over prefer users to use something similar to
> should_client_block(). But if enough users of mp _want_ to have
> $r->content, we need a _good_ way of achieving that (or a better excuse
> not to do it: "nevermind that, just use libapreq2 and it'll be even
> simpler/faster")

I don't have any objections to having $r->content in mp2; but
I do think it's a very bad idea to make it "too" smart.  By NOT
having it automatically stuff something in a request note,
you actually discourage people from abusing it (e.g. by attempting 
to use $r->content outside of the actual content-handler).

-- 
Joe Schaefer


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Philippe M. Chiasson wrote:
> On Tue, 2003-05-20 at 06:55, Stas Bekman wrote:
> 
>>Philippe M. Chiasson wrote:
>>
>>
>>>I would many times over prefer users to use something similar to
>>>should_client_block(). But if enough users of mp _want_ to have
>>>$r->content, we need a _good_ way of achieving that (or a better excuse
>>>not to do it: "nevermind that, just use libapreq2 and it'll be even
>>>simpler/faster")
>>
>>Phillippe, let me repeat again: get_client_block API is broken (See the recent 
>>thread on httpd-dev), and rumours go that it may be deprecated and totally 
>>removed. So the suggested $r->content is really a replacement for 
>>get_client_block that works.
> 
> 
> I meant something similar to get_client_block et all. I know what busted
> state that API is ;-)
> 
> I just meant that for that kind of data pulled from the client, I would
> prefer if we kept a way for the user to read that data in chunks,
> instead as in a whole buffer.
> 
> If I understand how Joe explained the mechanism of libapreq2, it's
> exactly what I mean.
> 
> If you have 10Mb of POST data coming in, there is no reason why your
> process should grow of that amount, if you are never interested in more
> than a specific chunk, like the value of one header or another.

Sure, agreed. Let's see if get_client_block get fixed in the httpd-core, if 
not we will simply write our own the right way. Still we may have to deal with 
older httpd versions.


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Tue, 2003-05-20 at 06:55, Stas Bekman wrote:
> Philippe M. Chiasson wrote:
> 
> > I would many times over prefer users to use something similar to
> > should_client_block(). But if enough users of mp _want_ to have
> > $r->content, we need a _good_ way of achieving that (or a better excuse
> > not to do it: "nevermind that, just use libapreq2 and it'll be even
> > simpler/faster")
> 
> Phillippe, let me repeat again: get_client_block API is broken (See the recent 
> thread on httpd-dev), and rumours go that it may be deprecated and totally 
> removed. So the suggested $r->content is really a replacement for 
> get_client_block that works.

I meant something similar to get_client_block et all. I know what busted
state that API is ;-)

I just meant that for that kind of data pulled from the client, I would
prefer if we kept a way for the user to read that data in chunks,
instead as in a whole buffer.

If I understand how Joe explained the mechanism of libapreq2, it's
exactly what I mean.

If you have 10Mb of POST data coming in, there is no reason why your
process should grow of that amount, if you are never interested in more
than a specific chunk, like the value of one header or another.

> __________________________________________________________________
> Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/     mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org   http://ticketmaster.com
-- 
-- -----------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'

Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Philippe M. Chiasson wrote:

> I would many times over prefer users to use something similar to
> should_client_block(). But if enough users of mp _want_ to have
> $r->content, we need a _good_ way of achieving that (or a better excuse
> not to do it: "nevermind that, just use libapreq2 and it'll be even
> simpler/faster")

Phillippe, let me repeat again: get_client_block API is broken (See the recent 
thread on httpd-dev), and rumours go that it may be deprecated and totally 
removed. So the suggested $r->content is really a replacement for 
get_client_block that works.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Mon, 2003-05-19 at 10:50, Joe Schaefer wrote:
> "Philippe M. Chiasson" <go...@cpan.org> writes:
> 
> [...]
> 
> > Well, the thing that bothers me is this.
> > 
> > My code uses $r->content and all works fine. Then I install some aaa
> > handler that calls $r->content before me. All of a sudden, my existing
> > code doesn't work anymore. 
> 
> That's exactly why people shouldn't be using $r->content,
> especially if they're coding something besides a content-handler.

Absolutely! I would tend to agree with Doug's previous reasons why
$r->content shouldn't exist in mp. But if we do chose to implement,
I think it should at least be a bit more 'smarter' that it's implemented
in mp1.

> Encouraging authors to pull the POST data out of the filter chain
> prematurely, and maybe stuff it into a request note, is IMHO not 
> goodness.  Especially not for an aaa handler; it'll certainly
> wind up breaking modules that are based on apreq-2.

Well, then what is the better solution if 2 different modules need
access to the POSTed data (I didn't look at how apreq handles this) ?

I agree that stuffing potentially Megabytes in pnotes is far from ideal.

I would many times over prefer users to use something similar to
should_client_block(). But if enough users of mp _want_ to have
$r->content, we need a _good_ way of achieving that (or a better excuse
not to do it: "nevermind that, just use libapreq2 and it'll be even
simpler/faster")

Gozer out.

-- 
-- -----------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'

Re: [patch] C implementation of $r->content + rfc on the name

Posted by Joe Schaefer <jo...@sunstarsys.com>.
"Philippe M. Chiasson" <go...@cpan.org> writes:

[...]

> Well, the thing that bothers me is this.
> 
> My code uses $r->content and all works fine. Then I install some aaa
> handler that calls $r->content before me. All of a sudden, my existing
> code doesn't work anymore. 

That's exactly why people shouldn't be using $r->content,
especially if they're coding something besides a content-handler.
Encouraging authors to pull the POST data out of the filter chain
prematurely, and maybe stuff it into a request note, is IMHO not 
goodness.  Especially not for an aaa handler; it'll certainly
wind up breaking modules that are based on apreq-2.

-- 
Joe Schaefer


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
>>I thought that since this is an input stream, chances are that 99.9% of the 
>>traffic will be plain HEAP buckets. POST data is always coming fully attached. 
>>So it should be safe to force using the length.
> 
> 
> I didn't check the code, but I do seem to remember that
> core_input_filter will insert a socket bucket (the tcp connection) in
> the input chain.
> 
> And I would expect that callign apr_brigade_length on that with the '1'
> flag could potentially result in a blocking call to apr_bucket_read if
> apr_brigade_length is reading in APR_BLOCK_READ mode.
> 
> It does make sense in a way. If the data is coming from a slow client,
> you'll need to read it all in before you can figure out what it's size
> is.

Then it's a bad idea. Your other suggestion to have a good get_client_block 
seems to be ok. But I think that it's faster to process bucket brigades 
directly, since you never need to split buckets.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Tue, 2003-05-20 at 06:57, Stas Bekman wrote:
> Philippe M. Chiasson wrote:
> 
> >>>Why not implement your own pflatten equivalent
> >>>
> >>>apr_status_t modperl_brigade_sv_flatten(pTHX_ apr_bucket_brigade *bb, SV
> >>>*sv) {
> >>>                                               
> >>>{
> >>>    apr_off_t actual;
> >>>    apr_size_t total;
> >>>    apr_status_t rv;
> >>>
> >>>    /* XXX: 1: triggers reads on unknown size buckets */
> >>>    apr_brigade_length(bb, 1, &actual);
> >>>    total = (apr_size_t)actual;
> >>>    
> >>>    return apr_brigade_flatten(bb, SvGROW(sv, total), &total);
> >>>}
> >>>
> >>>Something like that, to at least avoid allocating the storage twice.
> >>
> >>right, I've missed the _length API :(
> > 
> > 
> > And it should be used carefully, as to figure out the length of a
> > brigade, all buckets with unknown length (-1), i.e. network buckets,
> > will have to be read first (i.e. network reads) before their size can be
> > assessed. That's why there is a 1/0 flag in _length to specify if you
> > want the 'exact' size, or just the size of what's known.
> 
> I thought that since this is an input stream, chances are that 99.9% of the 
> traffic will be plain HEAP buckets. POST data is always coming fully attached. 
> So it should be safe to force using the length.

I didn't check the code, but I do seem to remember that
core_input_filter will insert a socket bucket (the tcp connection) in
the input chain.

And I would expect that callign apr_brigade_length on that with the '1'
flag could potentially result in a blocking call to apr_bucket_read if
apr_brigade_length is reading in APR_BLOCK_READ mode.

It does make sense in a way. If the data is coming from a slow client,
you'll need to read it all in before you can figure out what it's size
is.
> __________________________________________________________________
> Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/     mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org   http://ticketmaster.com
-- 
-- -----------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'

Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Philippe M. Chiasson wrote:

>>>Why not implement your own pflatten equivalent
>>>
>>>apr_status_t modperl_brigade_sv_flatten(pTHX_ apr_bucket_brigade *bb, SV
>>>*sv) {
>>>                                               
>>>{
>>>    apr_off_t actual;
>>>    apr_size_t total;
>>>    apr_status_t rv;
>>>
>>>    /* XXX: 1: triggers reads on unknown size buckets */
>>>    apr_brigade_length(bb, 1, &actual);
>>>    total = (apr_size_t)actual;
>>>    
>>>    return apr_brigade_flatten(bb, SvGROW(sv, total), &total);
>>>}
>>>
>>>Something like that, to at least avoid allocating the storage twice.
>>
>>right, I've missed the _length API :(
> 
> 
> And it should be used carefully, as to figure out the length of a
> brigade, all buckets with unknown length (-1), i.e. network buckets,
> will have to be read first (i.e. network reads) before their size can be
> assessed. That's why there is a 1/0 flag in _length to specify if you
> want the 'exact' size, or just the size of what's known.

I thought that since this is an input stream, chances are that 99.9% of the 
traffic will be plain HEAP buckets. POST data is always coming fully attached. 
So it should be safe to force using the length.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Mon, 2003-05-19 at 07:15, Stas Bekman wrote:
> Philippe M. Chiasson wrote:
> 
> >>+        rc = apr_brigade_pflatten(bb, &buffer, &bufsiz, r->pool);
> > 
> > 
> > No need to set bufsiz to HUGE_STRING_LEN (8192 bytes), since from the
> > source of apr_brigade_pflatten, it will allocate apr_brigade_length(bb)
> > from the pool, then overwrite bufsize with what it read.
> 
> Correct, I've first tried to implement it with _flatter (without the pool) and 
> then forgot to remove that.
> 
> >>+        if (rc != APR_SUCCESS) {
> >>+            apr_brigade_destroy(bb);
> >>+            Perl_croak(aTHX_ modperl_apr_strerror(rc));
> >>+        }
> >>+
> >>+        // XXX: more efficient way?
> >>+        sv_catpvn(sv, buffer, bufsiz);
> > 
> > 
> > So if we have a file upload POST, for example of 4MB,
> > apr_brigade_pflatten() will pcalloc 4MB from the pool, then sv_catpvn
> > will in turn malloc another 4MB.. Doesn't sound super cool.
> 
> True.
> 
> > Why not implement your own pflatten equivalent
> > 
> > apr_status_t modperl_brigade_sv_flatten(pTHX_ apr_bucket_brigade *bb, SV
> > *sv) {
> >                                                
> > {
> >     apr_off_t actual;
> >     apr_size_t total;
> >     apr_status_t rv;
> > 
> >     /* XXX: 1: triggers reads on unknown size buckets */
> >     apr_brigade_length(bb, 1, &actual);
> >     total = (apr_size_t)actual;
> >     
> >     return apr_brigade_flatten(bb, SvGROW(sv, total), &total);
> > }
> > 
> > Something like that, to at least avoid allocating the storage twice.
> 
> right, I've missed the _length API :(

And it should be used carefully, as to figure out the length of a
brigade, all buckets with unknown length (-1), i.e. network buckets,
will have to be read first (i.e. network reads) before their size can be
assessed. That's why there is a 1/0 flag in _length to specify if you
want the 'exact' size, or just the size of what's known.

> >>+        apr_brigade_cleanup(bb);
> >>+    }
> >>+    while (!seen_eos);
> > 
> > 
> > Seems to me like a potential infinite loop waiting to happen. What if we
> > never see an EOS ?
> 
> That was copied from mod_cgi. If we have an infinite loop, than one of the 
> custom filters is broken.

I see.

> >>+    apr_brigade_destroy(bb);
> >>+
> >>+    return sv;
> >>+}
> > 
> > 
> > Hey! Shouldn't we stash that sv in $r somewhere, because it would be
> > nice to be able to read $r->content more than only once.
> 
> I don't think we should, this is how it was in mp1. If you want to stash it 
> away, you do it in your code.

Well, the thing that bothers me is this.

My code uses $r->content and all works fine. Then I install some aaa
handler that calls $r->content before me. All of a sudden, my existing
code doesn't work anymore. (I know it's been like that already with
mp1). Why not just stash that SV in pnotes or something anyhow.

Just my 0.02$

> __________________________________________________________________
> Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/     mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org   http://ticketmaster.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
> For additional commands, e-mail: dev-help@perl.apache.org
-- 
-- -----------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'

Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Philippe M. Chiasson wrote:

>>+        rc = apr_brigade_pflatten(bb, &buffer, &bufsiz, r->pool);
> 
> 
> No need to set bufsiz to HUGE_STRING_LEN (8192 bytes), since from the
> source of apr_brigade_pflatten, it will allocate apr_brigade_length(bb)
> from the pool, then overwrite bufsize with what it read.

Correct, I've first tried to implement it with _flatter (without the pool) and 
then forgot to remove that.

>>+        if (rc != APR_SUCCESS) {
>>+            apr_brigade_destroy(bb);
>>+            Perl_croak(aTHX_ modperl_apr_strerror(rc));
>>+        }
>>+
>>+        // XXX: more efficient way?
>>+        sv_catpvn(sv, buffer, bufsiz);
> 
> 
> So if we have a file upload POST, for example of 4MB,
> apr_brigade_pflatten() will pcalloc 4MB from the pool, then sv_catpvn
> will in turn malloc another 4MB.. Doesn't sound super cool.

True.

> Why not implement your own pflatten equivalent
> 
> apr_status_t modperl_brigade_sv_flatten(pTHX_ apr_bucket_brigade *bb, SV
> *sv) {
>                                                
> {
>     apr_off_t actual;
>     apr_size_t total;
>     apr_status_t rv;
> 
>     /* XXX: 1: triggers reads on unknown size buckets */
>     apr_brigade_length(bb, 1, &actual);
>     total = (apr_size_t)actual;
>     
>     return apr_brigade_flatten(bb, SvGROW(sv, total), &total);
> }
> 
> Something like that, to at least avoid allocating the storage twice.

right, I've missed the _length API :(

>>+        apr_brigade_cleanup(bb);
>>+    }
>>+    while (!seen_eos);
> 
> 
> Seems to me like a potential infinite loop waiting to happen. What if we
> never see an EOS ?

That was copied from mod_cgi. If we have an infinite loop, than one of the 
custom filters is broken.

>>+    apr_brigade_destroy(bb);
>>+
>>+    return sv;
>>+}
> 
> 
> Hey! Shouldn't we stash that sv in $r somewhere, because it would be
> nice to be able to read $r->content more than only once.

I don't think we should, this is how it was in mp1. If you want to stash it 
away, you do it in your code.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by "Philippe M. Chiasson" <go...@cpan.org>.
On Thu, 2003-05-15 at 16:04, Stas Bekman wrote:
> recently I've learn that the implementation of ap_get_client_block is buggy. 
> For example it can't handle situations where EOS arrives in the bucket brigade 
> as data. There was a short followup on the httpd-dev list, which has quickly 
> died. But the fact remains, we can't rely on using ap_get_client_block even in 
> Apache::compat. I've already rewritten ModPerl::Test::read_post to read the 
> POST data using bbs and it works fine. I was just about to replace the 
> $r->content implementation in Apache::compat when I've realized that it's too 
> slow and should be done in C.
> 
> Notice that this C implementation is the same as $r->content() in the scalar 
> context in mp1, though it handles chunked encoding and doesn't rely on C-L 
> header. It simply delegates these headaches to httpd. This C implementation 
> just reads the POST data, just like $r->args returns just the QUERY_STRING. It 
> won't handle any multi-part POST. Though I think it might be useful and faster 
> then any other fully-fledged implementation, when all you want is to grab the 
> POSTed data as is.
> 
> Let me know if you have any problems with that. The only part is I hate is 
> that since we have to keep the same name in Apache/compat.pm, once someone 
> loads Apache::compat, $r->content is going to be overloaded to handle list 
> context as well. May be we should give this new mp2 function a new name? e.g. 
> $r->body?
> 
> 
> Index: lib/Apache/compat.pm
> ===================================================================
> RCS file: /home/cvs/modperl-2.0/lib/Apache/compat.pm,v
> retrieving revision 1.85
> diff -u -r1.85 compat.pm
> --- lib/Apache/compat.pm	11 Apr 2003 07:34:03 -0000	1.85
> +++ lib/Apache/compat.pm	15 May 2003 07:52:48 -0000
> @@ -285,24 +285,15 @@
> 
>   use constant IOBUFSIZE => 8192;
> 
> -sub content {
> -    my $r = shift;
> +{
> +    my $content_sub = *Apache::RequestRec::content{CODE};
> +    *Apache::RequestRec::content = sub {
> +        my $r = shift;
> 
> -    $r->setup_client_block;
> -
> -    return undef unless $r->should_client_block;
> -
> -    my $data = '';
> -    my $buf;
> -    while (my $read_len = $r->get_client_block($buf, IOBUFSIZE)) {
> -        if ($read_len == -1) {
> -            die "some error while reading with get_client_block";
> -        }
> -        $data .= $buf;
> +        my $data = $r->$content_sub;
> +        return $data unless wantarray;
> +        return $r->parse_args($data);
>       }
> -
> -    return $data unless wantarray;
> -    return $r->parse_args($data);
>   }
> 
>   sub clear_rgy_endav {
> Index: xs/Apache/RequestIO/Apache__RequestIO.h
> ===================================================================
> RCS file: /home/cvs/modperl-2.0/xs/Apache/RequestIO/Apache__RequestIO.h,v
> retrieving revision 1.37
> diff -u -r1.37 Apache__RequestIO.h
> --- xs/Apache/RequestIO/Apache__RequestIO.h	14 Mar 2003 05:33:19 -0000	1.37
> +++ xs/Apache/RequestIO/Apache__RequestIO.h	15 May 2003 07:52:48 -0000
> @@ -245,6 +245,60 @@
>       return total;
>   }
> 
> +
> +/* get the POSTed body as-is */
> +static SV *mpxs_Apache__RequestRec_content(pTHX_ request_rec *r)
> +{
> +    SV *sv = newSVpvn("", 0);
> +    int rc;
> +    int seen_eos = 0;
> +    apr_bucket_brigade *bb = apr_brigade_create(r->pool,
> +                                                r->connection->bucket_alloc);
> +
> +    do {
> +        char *buffer;
> +        apr_bucket *b;
> +        apr_size_t bufsiz = HUGE_STRING_LEN;

See next comment

> +        rc = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
> +                            APR_BLOCK_READ, HUGE_STRING_LEN);
> +        if (rc != APR_SUCCESS) {
> +            apr_brigade_destroy(bb);
> +            Perl_croak(aTHX_ modperl_apr_strerror(rc));
> +        }
> +
> +        /* If this fails, it means that a filter is written
> +         * incorrectly and that it needs to learn how to properly
> +         * handle APR_BLOCK_READ requests by returning data when
> +         * requested.
> +         */
> +        AP_DEBUG_ASSERT(!APR_BRIGADE_EMPTY(bb));
> +
> +        /* search for EOS */
> +        APR_BRIGADE_FOREACH(b, bb) {
> +            if (APR_BUCKET_IS_EOS(b)) {
> +                seen_eos = 1;
> +                break;
> +            }
> +        }
> +
> +        rc = apr_brigade_pflatten(bb, &buffer, &bufsiz, r->pool);

No need to set bufsiz to HUGE_STRING_LEN (8192 bytes), since from the
source of apr_brigade_pflatten, it will allocate apr_brigade_length(bb)
from the pool, then overwrite bufsize with what it read.

> +        if (rc != APR_SUCCESS) {
> +            apr_brigade_destroy(bb);
> +            Perl_croak(aTHX_ modperl_apr_strerror(rc));
> +        }
> +
> +        // XXX: more efficient way?
> +        sv_catpvn(sv, buffer, bufsiz);

So if we have a file upload POST, for example of 4MB,
apr_brigade_pflatten() will pcalloc 4MB from the pool, then sv_catpvn
will in turn malloc another 4MB.. Doesn't sound super cool.

Why not implement your own pflatten equivalent

apr_status_t modperl_brigade_sv_flatten(pTHX_ apr_bucket_brigade *bb, SV
*sv) {
                                               
{
    apr_off_t actual;
    apr_size_t total;
    apr_status_t rv;

    /* XXX: 1: triggers reads on unknown size buckets */
    apr_brigade_length(bb, 1, &actual);
    total = (apr_size_t)actual;
    
    return apr_brigade_flatten(bb, SvGROW(sv, total), &total);
}

Something like that, to at least avoid allocating the storage twice.


> +        apr_brigade_cleanup(bb);
> +    }
> +    while (!seen_eos);

Seems to me like a potential infinite loop waiting to happen. What if we
never see an EOS ?

> +    apr_brigade_destroy(bb);
> +
> +    return sv;
> +}

Hey! Shouldn't we stash that sv in $r somewhere, because it would be
nice to be able to read $r->content more than only once.


>   static MP_INLINE
>   SV *mpxs_Apache__RequestRec_GETC(pTHX_ request_rec *r)
>   {
> Index: xs/maps/modperl_functions.map
> ===================================================================
> RCS file: /home/cvs/modperl-2.0/xs/maps/modperl_functions.map,v
> retrieving revision 1.56
> diff -u -r1.56 modperl_functions.map
> --- xs/maps/modperl_functions.map	1 Apr 2003 05:20:50 -0000	1.56
> +++ xs/maps/modperl_functions.map	15 May 2003 07:52:48 -0000
> @@ -45,6 +45,7 @@
>    SV *:DEFINE_UNTIE    | | request_rec *:r, int:refcnt
>    mpxs_Apache__RequestRec_sendfile | | r, filename=r->filename, offset=0, len=0
>    mpxs_Apache__RequestRec_read | | r, buffer, bufsiz, offset=0
> + mpxs_Apache__RequestRec_content | | r
>    long:DEFINE_READ | | request_rec *:r, SV *:buffer, int:bufsiz, int:offset=0
>    mpxs_Apache__RequestRec_write | | r, buffer, bufsiz=-1, offset=0
>    mpxs_Apache__RequestRec_print | | ...
> Index: xs/tables/current/ModPerl/FunctionTable.pm
> ===================================================================
> RCS file: /home/cvs/modperl-2.0/xs/tables/current/ModPerl/FunctionTable.pm,v
> retrieving revision 1.114
> diff -u -r1.114 FunctionTable.pm
> --- xs/tables/current/ModPerl/FunctionTable.pm	12 May 2003 13:00:15 -0000	1.114
> +++ xs/tables/current/ModPerl/FunctionTable.pm	15 May 2003 07:52:48 -0000
> @@ -5595,6 +5595,23 @@
>       ]
>     },
>     {
> +    'return_type' => 'SV *',
> +    'name' => 'mpxs_Apache__RequestRec_content',
> +    'attr' => [
> +      'static'
> +    ],
> +    'args' => [
> +      {
> +        'type' => 'PerlInterpreter *',
> +        'name' => 'my_perl'
> +      },
> +      {
> +        'type' => 'request_rec *',
> +        'name' => 'r'
> +      },
> +    ]
> +  },
> +  {
>       'return_type' => 'int',
>       'name' => 'mpxs_Apache__RequestRec_OPEN',
>       'args' => [
> 
> __________________________________________________________________
> Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
> http://stason.org/     mod_perl Guide ---> http://perl.apache.org
> mailto:stas@stason.org http://use.perl.org http://apacheweek.com
> http://modperlbook.org http://apache.org   http://ticketmaster.com
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
> For additional commands, e-mail: dev-help@perl.apache.org
-- 
-- -----------------------------------------------------------------------------
Philippe M. Chiasson /gozer\@(cpan|ectoplasm)\.org/ 88C3A5A5 (122FF51B/C634E37B)
http://gozer.ectoplasm.org/    F9BF E0C2 480E 7680 1AE5 3631 CB32 A107 88C3 A5A5
Q: It is impossible to make anything foolproof because fools are so ingenious.
perl -e'$$=\${gozer};{$_=unpack(P7,pack(L,$$));/^JAm_pH\n$/&&print||$$++&&redo}'

Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
Geoffrey Young wrote:
> 
> 
> Stas Bekman wrote:
> 
>> I forgot to mention this URL, where Doug has stated why we shouldn't 
>> implement $r->content (and $r->args in list context), but to delegate 
>> it to CGI.pm, Apache::Request and other util modules.
>> http://marc.theaimsgroup.com/?l=apache-modperl-dev&m=102182866024188&w=2
>>
>> I think there is no interest collisions here, as the new method simply 
>> returns the whole POSTed body as is and will be documented as such.
> 
> 
> I'm inclined to agree with doug, namely:
> 
> - if one wishes to simply read POST data, there is the more modern
>   {setup,should,get}_client_block api, and even more modern filter
>   api.  along with continued support for read(STDIN, ...) and
>   $r->read($buf, $r->headers_in->{'content-length'})
> 
> my preference is to stick to the Apache API as much as possible, so for 
> me I'd probably rather see the *client_block_api opened up or documented 
> (depending on its current status).  the alternative is to tell people to 
> use CGI.pm (which ships standard with perl, so it's not a big issue) 
> until libapreq officially supports mp2, or return to the old STDIN stuff 
> everyone used to do back in the old days :)
> 
> really, dropping $r->content is just a matter of education - it's not 
> like there aren't other ways to get POST data already (not that I've 
> tried ;)

Have you read the first sentence of my post:

recently I've learned that the implementation of ap_get_client_block is buggy.

What I've forgotten to mention that Justin suggests to deprecate and remove 
this API from 2.2.

We also need to rewrite all the code where it's used. Because currently 
mod_perl 2.0, using those methods, doesn't quite work when filters, returning 
EOS in the bb, are used.

Using the filter bucket brigade is the way to go, but it's slower doing that 
in perl. So the only suggestion I was making is doing it in C. CGI.pm's 
implementation is even slower, since it asks to read data in specified chunks, 
so everytime the chunk ends in the middle of bucket, you have to split buckets.

The suggested solution was to slurp the data faster (with performance 
improvements from gozer). The new $r->content won't be useful to most people 
anyways, because it doesn't parse the body. We could even extend it to use the 
POST limit feature and then CGI.pm could use it as well, instead of reading 
from STDIN. Resulting in faster CGI.pm.



__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

Stas Bekman wrote:
> I forgot to mention this URL, where Doug has stated why we shouldn't 
> implement $r->content (and $r->args in list context), but to delegate it 
> to CGI.pm, Apache::Request and other util modules.
> http://marc.theaimsgroup.com/?l=apache-modperl-dev&m=102182866024188&w=2
> 
> I think there is no interest collisions here, as the new method simply 
> returns the whole POSTed body as is and will be documented as such.

I'm inclined to agree with doug, namely:

- if one wishes to simply read POST data, there is the more modern
   {setup,should,get}_client_block api, and even more modern filter
   api.  along with continued support for read(STDIN, ...) and
   $r->read($buf, $r->headers_in->{'content-length'})

my preference is to stick to the Apache API as much as possible, so for me 
I'd probably rather see the *client_block_api opened up or documented 
(depending on its current status).  the alternative is to tell people to use 
CGI.pm (which ships standard with perl, so it's not a big issue) until 
libapreq officially supports mp2, or return to the old STDIN stuff everyone 
used to do back in the old days :)

really, dropping $r->content is just a matter of education - it's not like 
there aren't other ways to get POST data already (not that I've tried ;)

--Geoff


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org


Re: [patch] C implementation of $r->content + rfc on the name

Posted by Stas Bekman <st...@stason.org>.
I forgot to mention this URL, where Doug has stated why we shouldn't implement 
$r->content (and $r->args in list context), but to delegate it to CGI.pm, 
Apache::Request and other util modules.
http://marc.theaimsgroup.com/?l=apache-modperl-dev&m=102182866024188&w=2

I think there is no interest collisions here, as the new method simply returns 
the whole POSTed body as is and will be documented as such.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@perl.apache.org
For additional commands, e-mail: dev-help@perl.apache.org