/extras/perl/site_perl/LWP/Protocol/GHTTP.pm

http://github.com/perigrin/android-scripting-environment-perl · Perl · 73 lines · 44 code · 18 blank · 11 comment · 5 complexity · e7ab93ead1b04608116fbe4f3607e66e MD5 · raw file

  1. package LWP::Protocol::GHTTP;
  2. # You can tell LWP to use this module for 'http' requests by running
  3. # code like this before you make requests:
  4. #
  5. # require LWP::Protocol::GHTTP;
  6. # LWP::Protocol::implementor('http', 'LWP::Protocol::GHTTP');
  7. #
  8. use strict;
  9. use vars qw(@ISA);
  10. require LWP::Protocol;
  11. @ISA=qw(LWP::Protocol);
  12. require HTTP::Response;
  13. require HTTP::Status;
  14. use HTTP::GHTTP qw(METHOD_GET METHOD_HEAD METHOD_POST);
  15. my %METHOD =
  16. (
  17. GET => METHOD_GET,
  18. HEAD => METHOD_HEAD,
  19. POST => METHOD_POST,
  20. );
  21. sub request
  22. {
  23. my($self, $request, $proxy, $arg, $size, $timeout) = @_;
  24. my $method = $request->method;
  25. unless (exists $METHOD{$method}) {
  26. return HTTP::Response->new(&HTTP::Status::RC_BAD_REQUEST,
  27. "Bad method '$method'");
  28. }
  29. my $r = HTTP::GHTTP->new($request->uri);
  30. # XXX what headers for repeated headers here?
  31. $request->headers->scan(sub { $r->set_header(@_)});
  32. $r->set_type($METHOD{$method});
  33. # XXX should also deal with subroutine content.
  34. my $cref = $request->content_ref;
  35. $r->set_body($$cref) if length($$cref);
  36. # XXX is this right
  37. $r->set_proxy($proxy->as_string) if $proxy;
  38. $r->process_request;
  39. my $response = HTTP::Response->new($r->get_status);
  40. # XXX How can get the headers out of $r?? This way is too stupid.
  41. my @headers;
  42. eval {
  43. # Wrapped in eval because this method is not always available
  44. @headers = $r->get_headers;
  45. };
  46. @headers = qw(Date Connection Server Content-type
  47. Accept-Ranges Server
  48. Content-Length Last-Modified ETag) if $@;
  49. for (@headers) {
  50. my $v = $r->get_header($_);
  51. $response->header($_ => $v) if defined $v;
  52. }
  53. return $self->collect_once($arg, $response, $r->get_body);
  54. }
  55. 1;