home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / TestCommonPost.pm < prev    next >
Encoding:
Perl POD Document  |  2004-08-05  |  4.9 KB  |  199 lines

  1. # Copyright 2002-2004 The Apache Software Foundation
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. #     http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. package Apache::TestCommonPost;
  16.  
  17. use strict;
  18. use warnings FATAL => 'all';
  19.  
  20. use constant POST_HUGE => $ENV{APACHE_TEST_POST_HUGE} || 0;
  21.  
  22. use Apache::TestRequest ();
  23. use Apache::TestUtil qw(t_cmp t_debug);
  24. use Apache::Test qw(sok);
  25.  
  26. BEGIN {
  27.     my $use_inline = 0;
  28.  
  29.     eval {
  30.         #if Inline.pm and libcurl are available
  31.         #we can make this test about 3x faster,
  32.         #after the inlined code is compiled that is.
  33.         require Inline;
  34.         Inline->import(C => 'DATA', LIBS => ['-lcurl'],
  35.                        #CLEAN_AFTER_BUILD => 0,
  36.                        PREFIX => 'aptest_post_');
  37.         *request_init = \&curl_init;
  38.         *request_do   = \&curl_do;
  39.         $use_inline = 1;
  40.     } if POST_HUGE;
  41.  
  42.     if (POST_HUGE) {
  43.         if ($@) {
  44.             t_debug "tests will run faster with Inline and curl installed";
  45.             print $@;
  46.         }
  47.         else {
  48.             t_debug "using Inline and curl client";
  49.         }
  50.     }
  51.  
  52.     unless ($use_inline) {
  53.         t_debug "using LWP client";
  54.         #fallback to lwp
  55.         *request_init = \&lwp_init;
  56.         *request_do   = \&lwp_do;
  57.     }
  58. }
  59.  
  60. sub lwp_init {
  61.     use vars qw($UA $Location);
  62.     $UA = Apache::TestRequest::user_agent();
  63.     $Location = shift;
  64. }
  65.  
  66. sub lwp_do {
  67.     my $length = shift;
  68.  
  69.     my $request = HTTP::Request->new(POST => $Location);
  70.     $request->header('Content-length' => $length);
  71.  
  72.     if (LWP->VERSION >= 5.800) {
  73.         $request->content_ref(\('a' x $length));
  74.     } else {
  75.         # before LWP 5.800 there was no way to tell HTTP::Message not
  76.         # to copy the string, there is a settable content_ref since
  77.         # 5.800
  78.         use constant BUF_SIZE => 8192;
  79.  
  80.         my $remain = $length;
  81.         my $content = sub {
  82.             my $bytes = $remain < BUF_SIZE ? $remain : BUF_SIZE;
  83.             my $buf = 'a' x $bytes;
  84.             $remain -= $bytes;
  85.             $buf;
  86.         };
  87.  
  88.         $request->content($content);
  89.     }
  90.  
  91.  
  92.  
  93.     my $response = $UA->request($request);
  94.  
  95.     Apache::TestRequest::lwp_trace($response);
  96.  
  97.     return $response->content;
  98. }
  99.  
  100. my @run_post_test_small_sizes =
  101.   #1k..9k, 10k..50k, 100k
  102.   (1..9, 10..50, 100);
  103.  
  104. my @run_post_test_sizes = @run_post_test_small_sizes;
  105.  
  106. if (POST_HUGE) {
  107.     push @run_post_test_sizes,
  108.       #300k, 500k, 2Mb, 4Mb, 6Mb, 10Mb
  109.       300, 500, 2000, 4000, 6000, 10_000;
  110. }
  111.  
  112. sub Apache::TestCommon::run_post_test_sizes { @run_post_test_sizes }
  113.  
  114. sub Apache::TestCommon::run_post_test {
  115.     my $module = shift;
  116.     my $sizes = shift || \@run_post_test_sizes;
  117.  
  118.     my $location = Apache::TestRequest::resolve_url("/$module");
  119.  
  120.     request_init($location);
  121.  
  122.     for my $size (@$sizes) {
  123.         sok {
  124.             my $length = ($size * 1024);
  125.  
  126.             my $str = request_do($length);
  127.             chomp $str;
  128.  
  129.             t_cmp($length, $str, "length posted");
  130.         };
  131.     }
  132. }
  133.  
  134. 1;
  135. __DATA__
  136.  
  137. __C__
  138.  
  139. #include <curl/curl.h>
  140. #include <curl/easy.h>
  141.  
  142. static CURL *curl = NULL;
  143. static SV *response = Nullsv;
  144. static long total = 0;
  145.  
  146. static size_t my_curl_read(char *buffer, size_t size,
  147.                            size_t nitems, void *data)
  148. {
  149.     size_t bytes = nitems < total ? nitems : total;
  150.     memset(buffer, 'a', bytes);
  151.     total -= bytes;
  152.     return bytes;
  153. }
  154.  
  155. static size_t my_curl_write(char *buffer, size_t size,
  156.                             size_t nitems, void *data)
  157. {
  158.     sv_catpvn(response, buffer, nitems);
  159.     return nitems;
  160. }
  161.  
  162. void aptest_post_curl_init(char *url)
  163. {
  164.     char *proto = "HTTP/1.1"; /* curl default */
  165.     curl = curl_easy_init();
  166.     curl_easy_setopt(curl, CURLOPT_MUTE, 1);
  167.     curl_easy_setopt(curl, CURLOPT_URL, url);
  168.     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  169.     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  170.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_curl_read);
  171.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_curl_write);
  172.     if (!getenv("APACHE_TEST_HTTP11")) {
  173.         curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  174.         proto = "HTTP/1.0";
  175.     }
  176.     fprintf(stdout, "#CURL using protocol %s\n", proto);
  177.     fflush(stdout);
  178.     response = newSV(0);
  179. }
  180.  
  181. SV *aptest_post_curl_do(long len)
  182. {
  183.     sv_setpv(response, "");
  184.     total = len;
  185.     curl_easy_setopt(curl, CURLOPT_INFILESIZE, len);
  186.     curl_easy_perform(curl);
  187.     return SvREFCNT_inc(response);
  188. }
  189.  
  190. void aptest_post_END(void)
  191. {
  192.     if (response) {
  193.         SvREFCNT_dec(response);
  194.     }
  195.     if (curl) {
  196.         curl_easy_cleanup(curl);
  197.     }
  198. }
  199.