forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.java
More file actions
179 lines (158 loc) · 4.67 KB
/
Copy pathHttpRequest.java
File metadata and controls
179 lines (158 loc) · 4.67 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge.http;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import php.java.bridge.NotImplementedException;
/**
* A simple HTTP request implementation.
* @author jostb
*
*/
public class HttpRequest {
private HashMap headers;
private String method;
private String uri;
private InputStream in;
private byte [] buf;
private int bufStart = 0;
private int bufEnd = 0;
private int contentLength = -1;
private int count = 0;
/**
* Create a new HTTP request
* @param inputStream The InputStream
*/
public HttpRequest(InputStream inputStream) {
in = new HttpInputStream(new BufferedInputStream(inputStream));
headers = new HashMap();
}
/**
* Returns the header value
* @param string The header
* @return The header value
*/
public String getHeader(String string) {
return (String) headers.get(string);
}
/**
* Return the request Method
* @return GET, PUT or POST
*/
public String getMethod() {
return method;
}
/**
* Return the request URI
* @return The request URI
*/
public String getRequestURI() {
return uri;
}
/**
* Returns the InputStream
* @return The InputStream
*/
public InputStream getInputStream() {
return in;
}
/**
* Push back some bytes so that we can read them again.
* @param buf The buffer
* @param start The start position
* @param length The number of bytes
*/
public void pushBack(byte[] buf, int start, int length) {
this.buf = buf;
this.bufStart = start;
this.bufEnd = length+start;
}
private class HttpInputStream extends InputStream {
private InputStream in;
public HttpInputStream (InputStream in) {
this.in = in;
}
/* (non-Javadoc)
* @see java.io.InputStream#read()
*/
public int read() throws IOException {
throw new NotImplementedException();
}
public int read(byte[] b, int start, int length) throws IOException {
if(contentLength > -1 && count==contentLength) return -1;
if(bufStart!=bufEnd) {
if(bufEnd - bufStart < length) length = bufEnd - bufStart;
System.arraycopy(buf, bufStart, b, start, length);
bufStart += length;
count += length;
return length;
}
int n = in.read(b, start, length);
count += n;
return n;
}
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}
}
/**
* Add a header
* @param line A valid HTTP header, e.g. "Host: localhost"
*/
public void addHeader(String line) {
try {
headers.put
(line.substring(0, line.indexOf(":")).trim(),
line.substring(line.indexOf(":") + 1).trim());
}
catch (StringIndexOutOfBoundsException e) { /* not a valid header, assume method */
int i1=-1, i2=-1;
i1 = line.indexOf(' ');
if(i1!=-1) {
method = (line.substring(0, i1)).trim().toUpperCase().intern();
i2 = line.indexOf(' ', i1+1);
}
if(i2>i1) uri = line.substring(i1+1, i2);
}
}
/**
* Set the content length, it causes the InputStream to stop reading at some point.
* @param contentLength The content length.
* @see HttpRequest#getInputStream()
*/
public void setContentLength(int contentLength) {
this.count = 0;
this.contentLength = contentLength;
}
/** Close the request
* @throws IOException */
public void close() throws IOException {
try {
if(in!=null) in.close();
} finally {
in = null;
}
}
}