~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Net/HTTPResponse.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
=begin TML
 
4
 
 
5
---+ package Foswiki::Net::HTTPResponse
 
6
 
 
7
Fakeup of HTTP::Response for use when LWP is not available. Only implements
 
8
a small subset of the HTTP::Response methods:
 
9
| =code()= |
 
10
| =message()= |
 
11
| =header($field)= |
 
12
| =content()= |
 
13
| =is_error()= |
 
14
| =is_redirect()= |
 
15
 
 
16
See the documentation of HTTP::Response for information about the methods.
 
17
 
 
18
=cut
 
19
 
 
20
package Foswiki::Net::HTTPResponse;
 
21
 
 
22
use Assert;
 
23
 
 
24
sub new {
 
25
    my ( $class, $message ) = @_;
 
26
    return bless(
 
27
        {
 
28
            code    => 400,        # BAD REQUEST
 
29
            message => $message,
 
30
            headers => {},
 
31
        },
 
32
        $class
 
33
    );
 
34
}
 
35
 
 
36
sub parse {
 
37
    my ( $class, $text ) = @_;
 
38
    my $this = new( $class, 'Incomplete headers' );
 
39
 
 
40
    $text =~ s/\r\n/\n/gs;
 
41
    $text =~ s/\r/\n/gs;
 
42
    $text =~ s/^(.*?)\n\n//s;
 
43
    # untaint is OK, checked below
 
44
    my $httpHeader = $1;
 
45
    $this->{content} = $text;
 
46
    if ( $httpHeader =~ s/^HTTP\/[\d.]+\s(\d+)\s([^\r\n]*)//s ) {
 
47
        $this->{code}    = $1;
 
48
        $this->{message} = TAINT($2 || '');
 
49
    }
 
50
    while ( $httpHeader =~ s/^(\S*):\s*(.*)$//m ) {
 
51
 
 
52
        # implicit untaint is OK for header names,
 
53
        # but values need to be retainted
 
54
        $this->{headers}->{ lc($1) } = TAINT($2);
 
55
    }
 
56
    if ( $httpHeader =~ /\S/) {
 
57
        $this->{code}    = 400;
 
58
        $this->{message} = "Unparseable headers in response: $httpHeader";
 
59
    }
 
60
    return $this;
 
61
}
 
62
 
 
63
sub code {
 
64
    return shift->{code};
 
65
}
 
66
 
 
67
sub message {
 
68
    return shift->{message};
 
69
}
 
70
 
 
71
sub header {
 
72
    my ( $this, $h ) = @_;
 
73
    return $this->{headers}->{$h};
 
74
}
 
75
 
 
76
sub content {
 
77
    return shift->{content};
 
78
}
 
79
 
 
80
sub is_error {
 
81
    my $this = shift;
 
82
    return $this->{code} >= 400;
 
83
}
 
84
 
 
85
sub is_redirect {
 
86
    my $this = shift;
 
87
    return $this->{code} >= 300 && $this->{code} < 400;
 
88
}
 
89
 
 
90
1;
 
91
__DATA__
 
92
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
93
#
 
94
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
 
95
# Foswiki Contributors are listed in the AUTHORS file in the root
 
96
# of this distribution. NOTE: Please extend that file, not this notice.
 
97
#
 
98
# Additional copyrights apply to some or all of the code in this
 
99
# file as follows:
 
100
#
 
101
# Copyright (C) 2007 TWiki Contributors. All Rights Reserved.
 
102
# TWiki Contributors are listed in the AUTHORS file in the root
 
103
# of this distribution. NOTE: Please extend that file, not this notice.
 
104
#
 
105
# This program is free software; you can redistribute it and/or
 
106
# modify it under the terms of the GNU General Public License
 
107
# as published by the Free Software Foundation; either version 2
 
108
# of the License, or (at your option) any later version. For
 
109
# more details read LICENSE in the root of this distribution.
 
110
#
 
111
# This program is distributed in the hope that it will be useful,
 
112
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
113
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
114
#
 
115
# As per the GPL, removal of this notice is prohibited.