summaryrefslogtreecommitdiffstats
path: root/pki/base/util/src/com/netscape/cmsutil/http/HttpRequest.java
blob: 76232a2dc149f92247e89f442ceef098ad5568e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmsutil.http;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;


/**
 * Basic HTTP Request. not optimized for performance.
 * Set fields or parse from input.
 * Handles text content. 
 */
public class HttpRequest extends HttpMessage {
    public static final String GET = "GET";
    public static final String POST = "POST";
    public static final String HEAD = "HEAD";

    protected String mMethod = null;
    protected String mURI = null;
    protected String mHttpVers = null;

    /**
     * Instantiate a HttpResponse for write to http client.
     */
    public HttpRequest() {
        super();
    }

    /**
     * set set request method.
     */
    public void setMethod(String method)
        throws HttpProtocolException {
        if (!method.equals(GET) && !method.equals(HEAD) && 
            !method.equals(POST))
            throw new HttpProtocolException("No such method " + method);
        mMethod = method;
    }

    /**
     * set reason phrase.
     */
    public void setURI(String uri) {
        mURI = uri;
    }

    /**
     * write request to the http client
     */
    public void write(OutputStreamWriter writer)
        throws IOException {
        if (mMethod == null || mURI == null) {
            HttpProtocolException e = new HttpProtocolException(
                    "Http request method or uri not initialized");

            //e.printStackTrace();
            throw e;
        }

        mLine = mMethod + " " + mURI + " " + Http.HttpVers;
        super.write(writer);
    }

    /**
     * parse a http request from a http client
     */
    public void parse(BufferedReader reader)
        throws IOException {
        super.parse(reader);

        int method = mLine.indexOf(Http.SP);

        mMethod = mLine.substring(0, method);
        if (!mMethod.equals(GET) && !mMethod.equals(POST) && 
            !mMethod.equals(HEAD)) {
            reset();
            throw new HttpProtocolException("Bad Http request method");
        }	

        int uri = mLine.lastIndexOf(Http.SP);

        mURI = mLine.substring(method + 1, uri);

        mHttpVers = mLine.substring(uri + 1);
        if (!mHttpVers.equals("")) {
            if (!mHttpVers.equals(Http.Vers1_0) && 
                !mHttpVers.equals(Http.Vers1_1)) {
                reset();
                throw new HttpProtocolException("Bad Http version in request");
            }
        }
    }

    public void reset() {
        mMethod = null;
        mURI = null;
        mHttpVers = null;
        super.reset();
    }

    /**
     * get method
     */
    public String getMethod() {
        return mMethod;
    }

    /**
     * get reason phrase
     */
    public String getURI() {
        return mURI;
    }

    /**
     * get http version
     */
    public String getHttpVers() {
        return mHttpVers;
    }
}