Java Reference
In-Depth Information
}
String requestCommand
=
st
.
nextToken
();
requestURL
=
st
.
nextToken
();
String requestHTTPVersion
=
st
.
nextToken
();
System
.
out
.
println
(
"Request: Command "
+
requestCommand
+
", file "
+
requestURL
+
", version "
+
requestHTTPVersion
);
// First, check that rqCode is either GET or HEAD or ...
iif
(
"get"
.
equalsIgnoreCase
(
requestCommand
))
methodType
=
RequestType
.
RQ_GET
;
else
else
if
(
"head"
.
equalsIgnoreCase
(
requestCommand
))
methodType
=
RequestType
.
RQ_HEAD
;
else
else
if
(
"post"
.
equalsIgnoreCase
(
requestCommand
))
methodType
=
RequestType
.
RQ_POST
;
else
else
{
errorResponse
(
400
,
"invalid method: "
+
requestCommand
);
clntSock
.
close
();
return
return
;
}
// Read headers, up to the null line before the body,
// so the body can be read directly if it's a POST.
Map
<
String
,
String
>
headersMap
=
new
new
HashMap
<
String
,
String
>();
String hdrLine
;
while
while
((
hdrLine
=
is
.
readLine
()) !=
null
null
&&
hdrLine
.
length
() !=
0
) {
int
int
ix
;
iif
((
ix
=
hdrLine
.
indexOf
(
':'
)) != -
1
) {
String hdrName
=
hdrLine
.
substring
(
0
,
ix
);
String hdrValue
=
hdrLine
.
substring
(
ix
+
1
).
trim
();
Debug
.
println
(
"hdr"
,
hdrName
+
","
+
hdrValue
);
headersMap
.
put
(
hdrName
,
hdrValue
);
}
else
else
{
System
.
err
.
println
(
"INVALID HEADER: "
+
hdrLine
);
}
}
iif
(
methodType
==
RequestType
.
RQ_POST
) {
errorResponse
(
501
,
"Protocol not written yet"
);
clntSock
.
close
();
return
return
;
}
// Make a URL from the request
URL url
=
new
new
URL
(
requestURL
);
String protocol
=
url
.
getProtocol
();