home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / TestCommonPost.pm < prev    next >
Encoding:
Perl POD Document  |  2002-04-02  |  4.2 KB  |  175 lines

  1. package Apache::TestCommonPost;
  2.  
  3. use strict;
  4. use warnings FATAL => 'all';
  5.  
  6. use constant POST_HUGE => $ENV{APACHE_TEST_POST_HUGE} || 0;
  7.  
  8. use Apache::TestRequest ();
  9. use Apache::TestUtil qw(t_cmp t_debug);
  10. use Apache::Test qw(sok);
  11.  
  12. BEGIN {
  13.     my $use_inline = 0;
  14.  
  15.     eval {
  16.         #if Inline.pm and libcurl are available
  17.         #we can make this test about 3x faster,
  18.         #after the inlined code is compiled that is.
  19.         require Inline;
  20.         Inline->import(C => 'DATA', LIBS => ['-lcurl'],
  21.                        #CLEAN_AFTER_BUILD => 0,
  22.                        PREFIX => 'aptest_post_');
  23.         *request_init = \&curl_init;
  24.         *request_do   = \&curl_do;
  25.         $use_inline = 1;
  26.     } if POST_HUGE;
  27.  
  28.     if (POST_HUGE) {
  29.         if ($@) {
  30.             t_debug "tests will run faster with Inline and curl installed";
  31.             print $@;
  32.         }
  33.         else {
  34.             t_debug "using Inline and curl client";
  35.         }
  36.     }
  37.  
  38.     unless ($use_inline) {
  39.         t_debug "using LWP client";
  40.         #fallback to lwp
  41.         *request_init = \&lwp_init;
  42.         *request_do   = \&lwp_do;
  43.     }
  44. }
  45.  
  46. sub lwp_init {
  47.     use vars qw($UA $Location);
  48.     $UA = Apache::TestRequest::user_agent();
  49.     $Location = shift;
  50. }
  51.  
  52. sub lwp_do {
  53.     my $length = shift;
  54.     my $remain = $length;
  55.  
  56.     use constant BUF_SIZE => 8192;
  57.  
  58.     my $content = sub {
  59.         my $bytes = $remain < BUF_SIZE ? $remain : BUF_SIZE;
  60.         my $buf = 'a' x $bytes;
  61.         $remain -= $bytes;
  62.         $buf;
  63.     };
  64.  
  65.     my $request = HTTP::Request->new(POST => $Location);
  66.     $request->header('Content-length' => $length);
  67.     $request->content($content);
  68.  
  69.     my $response = $UA->request($request);
  70.  
  71.     Apache::TestRequest::lwp_trace($response);
  72.  
  73.     return $response->content;
  74. }
  75.  
  76. my @run_post_test_small_sizes =
  77.   #1k..9k, 10k..50k, 100k
  78.   (1..9, 10..50, 100);
  79.  
  80. my @run_post_test_sizes = @run_post_test_small_sizes;
  81.  
  82. if (POST_HUGE) {
  83.     push @run_post_test_sizes,
  84.       #300k, 500k, 2Mb, 4Mb, 6Mb, 10Mb
  85.       300, 500, 2000, 4000, 6000, 10_000;
  86. }
  87.  
  88. sub Apache::TestCommon::run_post_test_sizes { @run_post_test_sizes }
  89.  
  90. sub Apache::TestCommon::run_post_test {
  91.     my $module = shift;
  92.     my $sizes = shift || \@run_post_test_sizes;
  93.  
  94.     my $location = Apache::TestRequest::resolve_url("/$module");
  95.  
  96.     request_init($location);
  97.  
  98.     for my $size (@$sizes) {
  99.         sok {
  100.             my $length = ($size * 1024);
  101.  
  102.             my $str = request_do($length);
  103.             chomp $str;
  104.  
  105.             t_cmp($length, $str, "length posted");
  106.         };
  107.     }
  108. }
  109.  
  110. 1;
  111. __DATA__
  112.  
  113. __C__
  114.  
  115. #include <curl/curl.h>
  116. #include <curl/easy.h>
  117.  
  118. static CURL *curl = NULL;
  119. static SV *response = Nullsv;
  120. static long total = 0;
  121.  
  122. static size_t my_curl_read(char *buffer, size_t size,
  123.                            size_t nitems, void *data)
  124. {
  125.     size_t bytes = nitems < total ? nitems : total;
  126.     memset(buffer, 'a', bytes);
  127.     total -= bytes;
  128.     return bytes;
  129. }
  130.  
  131. static size_t my_curl_write(char *buffer, size_t size,
  132.                             size_t nitems, void *data)
  133. {
  134.     sv_catpvn(response, buffer, nitems);
  135.     return nitems;
  136. }
  137.  
  138. void aptest_post_curl_init(char *url)
  139. {
  140.     char *proto = "HTTP/1.1"; /* curl default */
  141.     curl = curl_easy_init();
  142.     curl_easy_setopt(curl, CURLOPT_MUTE, 1);
  143.     curl_easy_setopt(curl, CURLOPT_URL, url);
  144.     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  145.     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
  146.     curl_easy_setopt(curl, CURLOPT_READFUNCTION, my_curl_read);
  147.     curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_curl_write);
  148.     if (!getenv("APACHE_TEST_HTTP11")) {
  149.         curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  150.         proto = "HTTP/1.0";
  151.     }
  152.     fprintf(stdout, "#CURL using protocol %s\n", proto);
  153.     fflush(stdout);
  154.     response = newSV(0);
  155. }
  156.  
  157. SV *aptest_post_curl_do(long len)
  158. {
  159.     sv_setpv(response, "");
  160.     total = len;
  161.     curl_easy_setopt(curl, CURLOPT_INFILESIZE, len);
  162.     curl_easy_perform(curl);
  163.     return SvREFCNT_inc(response);
  164. }
  165.  
  166. void aptest_post_END(void)
  167. {
  168.     if (response) {
  169.         SvREFCNT_dec(response);
  170.     }
  171.     if (curl) {
  172.         curl_easy_cleanup(curl);
  173.     }
  174. }
  175.