-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTPRequest.java
127 lines (113 loc) · 3.66 KB
/
HTTPRequest.java
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
//
// Ewww Web Server -- A simple Java web-server.
// Copyright (C) 2005, Eron J. Hennessey
//
// 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; either version 2 of the License, or (at your option)
// any later version.
//
// 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.
//
import java.io.*;
import java.net.Socket;
/**
* A class that implements a thread whose specific role is to handle an HTTP
* request.
* @author Eron Hennessey
*/
public class HTTPRequest extends Thread
{
private Socket con_socket;
public HTTPRequest(Socket s)
{
con_socket = s;
}
/**
* The main procedure for this class.
*/
public void run()
{
try
{
BufferedReader sock_in = new BufferedReader(
new InputStreamReader(con_socket.getInputStream()));
PrintWriter sock_out = new PrintWriter(con_socket.getOutputStream());
// read the first line of the request. This should be a valid HTTP command, like GET.
String input_line = sock_in.readLine();
String tokens[] = input_line.split("\\s");
// check to see if it's a valid HTTP command.
if(tokens[0].equals("GET"))
{
processGETRequest(tokens, sock_in, sock_out);
}
else // not a valid HTTP command
{
sock_out.println("HTTP/1.0 400 Bad Request");
sock_out.println("");
}
sock_out.flush();
con_socket.close();
}
catch(IOException exc)
{
System.err.println("IO Exception caught in HTTPRequest!");
System.err.println("Thread exiting...");
return;
}
}
/**
* Handles reading an HTTP GET request.
*/
private void processGETRequest(String[] tokens, BufferedReader sock_in, PrintWriter sock_out)
{
// what file does the client want?
String desired_file = Ewww.document_root + tokens[1];
File file = new File(desired_file);
// if the file is a directory, look for the index.html file.
if(file.isDirectory())
{
desired_file = desired_file + "index.html";
file = new File(desired_file);
}
BufferedReader file_in = null;
try
{
file_in = new BufferedReader(new FileReader(file));
}
catch(FileNotFoundException exc)
{
sock_out.println("HTTP/1.0 404 Not Found");
sock_out.println("");
return;
}
// the file exists... send it!
sock_out.println("HTTP/1.0 200 OK");
sock_out.println("Content-Type: text/html");
sock_out.println("Content-Length: " + file.length());
sock_out.println("");
try
{
while(true)
{
String line = file_in.readLine();
if(line == null)
{
break;
}
sock_out.println(line);
}
}
catch(IOException exc)
{
return;
}
}
}