Java Reference
In-Depth Information
* this is probably quite inefficient, but simple. Need ThreadPool).
* Note that Hashtable methods *are* synchronized.
*/
private
private static
static
Map
<
String
,
Object
>
cache
=
new
new
HashMap
<
String
,
Object
>();
static
static
{
cache
.
put
(
""
,
"<html><body><b>Unknown server error</b>"
.
getBytes
());
}
/** Construct a Handler */
Handler
(
WebProxy parent
) {
this
this
.
parent
=
parent
;
}
protected
protected enum
enum
RequestType
{
RQ_INVALID
,
RQ_GET
,
RQ_HEAD
,
RQ_POST
};
String requestURL
;
public
public
void
void
process
(
Socket clntSock
) {
String request
;
// what Viewer sends us.
RequestType methodType
=
RequestType
.
RQ_INVALID
;
try
try
{
System
.
out
.
println
(
"Connection accepted from "
+
clntSock
.
getInetAddress
());
is
=
new
new
InputStreamReader
(
clntSock
.
getInputStream
()));
// Must do before any chance of errorResponse being called!
os
=
new
new
BufferedReader
(
new
new
PrintStream
(
clntSock
.
getOutputStream
());
request
=
is
.
readLine
();
iif
(
request
==
null
null
||
request
.
length
() ==
0
) {
// No point nattering: the sock died, nobody will hear
// us if we scream into cyberspace...
System
.
err
.
println
(
"The sock has died..."
);
return
return
;
}
// Use a StringTokenizer to break the request into its three parts:
// HTTP method, resource name, and HTTP version
StringTokenizer st
=
new
new
StringTokenizer
(
request
);
iif
(
st
.
countTokens
() !=
3
) {
errorResponse
(
444
,
"Unparseable input "
+
request
);
clntSock
.
close
();
return
return
;