blob: 1cad863036924f1c55d52537167cf7e5e0dabe0d (
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package com.c2kernel.utils.server;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import com.c2kernel.utils.Logger;
public class HTTPRequestHandler implements SocketHandler {
protected HashMap headers = new HashMap();
protected String method;
protected String resource;
protected String version;
protected String returnMIME;
protected String statusCode;
protected String postData;
protected final static String CRLF = "\r\n";
protected BufferedReader request;
protected Socket currentSocket = null;
public HTTPRequestHandler() { }
public String getName() {
return "HTTP Server";
}
public boolean isBusy() {
return (currentSocket!=null);
}
public void setSocket(Socket newSocket) {
currentSocket = newSocket;
}
public void shutdown() {
try {
if (currentSocket != null) currentSocket.close();
} catch (IOException e) {
Logger.error("Error shutting down HTTP connection");
}
currentSocket = null;
}
public void run() {
Thread.currentThread().setName("HTTP Request Handler");
try {
request = new BufferedReader(new InputStreamReader(currentSocket.getInputStream()));
// parse the request
boolean firstLine = true;
String headerLine;
do {
headerLine = request.readLine();
if (headerLine == null)
throw new IOException("Disconnected");
if (firstLine) { // request line
StringTokenizer params = new StringTokenizer(headerLine);
try {
method = params.nextToken();
resource = params.nextToken();
version = params.nextToken();
} catch (NoSuchElementException ex1) { } // incomplete request line - doesn't matter
firstLine = false;
}
else { // headers
int split = headerLine.indexOf(": ");
if (split >= 0)
headers.put(
headerLine.substring(0, split),
headerLine.substring(split+2)
);
}
} while (!headerLine.equals(CRLF) && !headerLine.equals(""));
String response = null;
try {
if (headers.containsKey("Content-length")) { // read POST data
StringBuffer postBuffer = new StringBuffer();
int received = 0; int length = Integer.parseInt((String)headers.get("Content-length"));
try {
while (received < length) {
String postLine = request.readLine();
postBuffer.append(postLine);
received+=postLine.length();
}
} catch (IOException ex) {
Logger.error("Error reading post data. "+received+" bytes of "+length+" received. ("+(received*100.0/length)+"%");
Logger.error(ex);
throw ex;
}
}
// Got the params, generate response
returnMIME = "text/xml";
statusCode = "200 OK";
response = processRequest();
} catch (Exception ex3) {
response = error("500 Server Error", ex3.getClass().getName()+"<br>"+ex3.getMessage());
}
System.out.println(new Date().toString()+" "+currentSocket.getInetAddress()+" "+method+" "+resource+" "+statusCode);
OutputStream output = currentSocket.getOutputStream();
statusCode = "HTTP/1.0 "+statusCode+CRLF;
output.write(statusCode.getBytes());
returnMIME = "Content-type: "+returnMIME+CRLF;
output.write(returnMIME.getBytes());
String contentLength = "Content-Length: "+response.length()+CRLF;
output.write(contentLength.getBytes());
// Possible: last mod?
// end of headers
output.write(CRLF.getBytes());
// write the content
output.write(response.getBytes());
request.close();
output.close();
currentSocket.close();
} catch (IOException ex2) {
Logger.error(ex2);
} // aborted connection probably
currentSocket = null;
}
/* This is a dummy method that doesn't support anything.
* Override it.
*/
public String processRequest() {
return error("501 Not Implemented", "The method "+method+" you have requested is not supported by this server.");
}
public String error(String code, String desc) {
statusCode = code;
returnMIME = "text/html";
return ("<HTML>" +
"<HEAD><TITLE>"+code+"</TITLE></HEAD>" +
"<BODY><h1>"+code+"</h1>" +
"<p>"+desc+
"<hr><p><small>Cristal Item HTTP server</small></BODY></HTML>");
}
}
|