Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 8a8eebd

Browse files
committed
httptester completed
1 parent e1f0d25 commit 8a8eebd

20 files changed

+399
-1
lines changed

Diff for: nbproject/configs/WebServer.properties

Whitespace-only changes.

Diff for: nbproject/project.properties

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ javadoc.splitindex=true
5757
javadoc.use=true
5858
javadoc.version=false
5959
javadoc.windowtitle=
60+
main.class=es.udc.redes.webserver.WebServer
6061
manifest.file=manifest.mf
6162
meta.inf.dir=${src.dir}/META-INF
6263
mkdist.disabled=false

Diff for: resources/access/access.log

Whitespace-only changes.

Diff for: resources/archives/LICENSE.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright [yyyy] [name of copyright owner]
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

Diff for: resources/archives/fic.png

13 KB
Loading

Diff for: resources/archives/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<html>
2+
<head>
3+
<title>
4+
A Simple HTML Document
5+
</title>
6+
</head>
7+
<body>
8+
<p>This is a very simple HTML document</p>
9+
<p>It only has two paragraphs</p>
10+
</body>
11+
</html>

Diff for: resources/archives/saludo.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
3+
4+
<head>
5+
<title> ¿A quién quieres saludar usando una página dinámica? </title>
6+
</head>
7+
8+
<body text="#000000" bgcolor="#ffffff">
9+
10+
<div align="center">
11+
<p><font color="#000099" size="+2" face="Arial, Helvetica, sans-serif">
12+
<b>¿A quién quieres saludar usando una página dinámica?</b></font><br>
13+
</p>
14+
</div>
15+
16+
<form action="MiServlet.do" method="get">
17+
<div align="center"> Nombre: <input type="text" name="nombre" />
18+
Primer apellido: <input type="text" name="primerApellido" />
19+
Segundo apellido: <input type="text" name="segundoApellido" /></div>
20+
21+
<div align="center"><p/><input type="submit" value="Salúdame"/></div>
22+
</form>
23+
24+
</body>
25+
26+
</html>

Diff for: resources/archives/udc.gif

4.15 KB
Loading

Diff for: resources/codes/200.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
3+
<head>
4+
<title>WebServer response</title>
5+
<link rel="icon HTML" href="https://www.udc.es/export/system/modules/es.udc.webcorp.template/resources/img/favicon/favicon_120.png">
6+
</head>
7+
8+
<body>
9+
<h1 style="font-family:monospace; color:#555555;" align="center">200 OK</h1>
10+
<hr color="color:#555555" size="6" w width="100%">
11+
<p style="font-family:verdana; color:#888888;" align="center">La solicitud ha tenido éxito.</p>
12+
</body>
13+
14+
</html>

Diff for: resources/codes/304.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
3+
<head>
4+
<title>WebServer response</title>
5+
<link rel="icon HTML" href="https://www.udc.es/export/system/modules/es.udc.webcorp.template/resources/img/favicon/favicon_120.png">
6+
</head>
7+
8+
<body>
9+
<h1 style="font-family:monospace; color:#555555;" align="center">304 Not Modified</h1>
10+
<hr color="color:#555555" size="6" w width="100%">
11+
<p style="font-family:verdana; color:#888888;" align="center">El recurso no se ha modificado.</p>
12+
</body>
13+
14+
</html>

Diff for: resources/codes/400.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
3+
<head>
4+
<title>WebServer response</title>
5+
<link rel="icon HTML" href="https://www.udc.es/export/system/modules/es.udc.webcorp.template/resources/img/favicon/favicon_120.png">
6+
</head>
7+
8+
<body>
9+
<h1 style="font-family:monospace; color:#555555;" align="center">400 Bad Request</h1>
10+
<hr color="color:#555555" size="6" w width="100%">
11+
<p style="font-family:verdana; color:#888888;" align="center">El servidor no pudo interpretar la solicitud.</p>
12+
</body>
13+
14+
</html>

Diff for: resources/codes/403.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
3+
<head>
4+
<title>WebServer response</title>
5+
<link rel="icon HTML" href="https://www.udc.es/export/system/modules/es.udc.webcorp.template/resources/img/favicon/favicon_120.png">
6+
</head>
7+
8+
<body>
9+
<h1 style="font-family:monospace; color:#555555;" align="center">403 Forbidden</h1>
10+
<hr color="color:#555555" size="6" w width="100%">
11+
<p style="font-family:verdana; color:#888888;" align="center">El cliente no posee los permisos necesarios para cierto contenido, por lo que el servidor está rechazando otorgar una respuesta apropiada.</p>
12+
</body>
13+
14+
</html>

Diff for: resources/codes/404.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
3+
<head>
4+
<title>WebServer response</title>
5+
<link rel="icon HTML" href="https://www.udc.es/export/system/modules/es.udc.webcorp.template/resources/img/favicon/favicon_120.png">
6+
</head>
7+
8+
<body>
9+
<h1 style="font-family:monospace; color:#555555;" align="center">404 Not Found</h1>
10+
<hr color="color:#555555" size="6" w width="100%">
11+
<p style="font-family:verdana; color:#888888;" align="center">El servidor no pudo encontrar el contenido solicitado.</p>
12+
</body>
13+
14+
</html>

Diff for: resources/codes/501.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
3+
<head>
4+
<title>WebServer response</title>
5+
<link rel="icon HTML" href="https://www.udc.es/export/system/modules/es.udc.webcorp.template/resources/img/favicon/favicon_120.png">
6+
</head>
7+
8+
<body>
9+
<h1 style="font-family:monospace; color:#555555;" align="center">501 Not Implemented</h1>
10+
<hr color="color:#555555" size="6" w width="100%">
11+
<p style="font-family:verdana; color:#888888;" align="center">El método solicitado no esta soportado por el servidor y no puede ser manejada.</p>
12+
</body>
13+
14+
</html>

Diff for: resources/config.properties

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#dir
2+
dir=/Users/Íker/Downloads/java-labs/resources
3+
#index
4+
index=index.html
5+
#allow
6+
allow=TRUE

Diff for: resources/errors/errors.log

Whitespace-only changes.

Diff for: resources/httptester.jar

2 MB
Binary file not shown.

Diff for: src/es/udc/redes/webserver/Code.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package es.udc.redes.webserver;
2+
3+
public enum Code {
4+
5+
OK("200 OK"),
6+
NOT_MODIFIED("304 Not Modified"),
7+
BAD_REQUEST("400 Bad Request"),
8+
FORBIDDEN("403 Forbidden"),
9+
NOT_FOUND("404 Not Found"),
10+
NOT_IMPLEMENTED("501 Not Implemented");
11+
12+
private final String estado;
13+
14+
private Code(String code) {
15+
this.estado = code;
16+
}
17+
18+
public String getEstado() {
19+
return estado;
20+
}
21+
22+
}

Diff for: src/es/udc/redes/webserver/ServerThread.java

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
package es.udc.redes.webserver;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.BufferedReader;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.io.OutputStream;
10+
import java.io.PrintWriter;
11+
import java.net.Socket;
12+
import java.net.SocketTimeoutException;
13+
import java.text.SimpleDateFormat;
14+
import java.util.Date;
15+
import java.util.StringTokenizer;
16+
17+
public class ServerThread extends Thread {
18+
19+
private Code code;
20+
private Date date;
21+
private File file = null;
22+
private final Socket socket;
23+
24+
public ServerThread(Socket s) {
25+
this.socket = s;
26+
}
27+
28+
@Override
29+
public void run() {
30+
31+
BufferedReader reader = null;
32+
PrintWriter writer = null;
33+
BufferedOutputStream dataOut = null;
34+
String fileRequest = null;
35+
36+
try {
37+
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
38+
writer = new PrintWriter(socket.getOutputStream());
39+
dataOut = new BufferedOutputStream(socket.getOutputStream());
40+
41+
String msg = reader.readLine();
42+
if (msg != null) {
43+
StringTokenizer parse = new StringTokenizer(msg);
44+
String method = parse.nextToken().toUpperCase();
45+
fileRequest = parse.nextToken().toLowerCase();
46+
47+
if (!method.equals("GET") && !method.equals("HEAD")) {
48+
code = Code.NOT_IMPLEMENTED;
49+
printHeader(writer, dataOut, "", fileRequest);
50+
}
51+
if (method.equals("GET")) {
52+
printHeader(writer, dataOut, "GET", fileRequest);
53+
}
54+
if (method.equals("HEAD")) {
55+
printHeader(writer, dataOut, "HEAD", fileRequest);
56+
}
57+
}
58+
if (reader != null) {
59+
reader.close();
60+
}
61+
if (writer != null) {
62+
writer.close();
63+
}
64+
} catch (SocketTimeoutException e) {
65+
System.err.println("Nothing received in 300 secs");
66+
} catch (IOException e) {
67+
System.err.println("Error: " + e.getMessage());
68+
} finally {
69+
try {
70+
if (reader != null) {
71+
reader.close();
72+
}
73+
if (writer != null) {
74+
writer.close();
75+
}
76+
if (dataOut != null) {
77+
dataOut.close();
78+
}
79+
if (socket != null) {
80+
socket.close();
81+
}
82+
} catch (IOException e) {
83+
throw new RuntimeException(e);
84+
}
85+
}
86+
87+
}
88+
89+
private void printHeader(PrintWriter printer, OutputStream dataOut, String metodo, String content) throws IOException {
90+
if (code == Code.NOT_IMPLEMENTED) {
91+
file = new File("resources/codes/501.html");
92+
} else {
93+
switch (content) {
94+
case "/fic.png":
95+
code = Code.OK;
96+
file = new File("resources/archives/fic.png");
97+
break;
98+
case "/index.html":
99+
code = Code.OK;
100+
file = new File("resources/archives/index.html");
101+
break;
102+
case "/license.txt":
103+
code = Code.OK;
104+
file = new File("resources/archives/LICENSE.txt");
105+
break;
106+
case "/saludo.html":
107+
code = Code.OK;
108+
file = new File("resources/archives/saludo.html");
109+
break;
110+
case "/udc.gif":
111+
code = Code.OK;
112+
file = new File("resources/archives/udc.gif");
113+
break;
114+
default:
115+
code = Code.NOT_FOUND;
116+
file = new File("resources/codes/404.html");
117+
break;
118+
}
119+
}
120+
121+
date = new Date();
122+
int length = (int) file.length();
123+
byte[] data = readerData(file, length);
124+
125+
printer.println("HTTP/1.0 " + code.getEstado());
126+
printer.println("Date: " + date);
127+
printer.println("Server: ");
128+
printer.println("Last-Modified: " + formatDate((int) file.lastModified()));
129+
printer.println("Content-Length: " + file.length());
130+
printer.println("Content-Type: " + getType(file));
131+
printer.println("");
132+
printer.flush();
133+
134+
if (code == Code.OK) {
135+
switch (metodo) {
136+
case "GET":
137+
dataOut.write(data, 0, length);
138+
break;
139+
case "HEAD":
140+
break;
141+
}
142+
} else {
143+
dataOut.write(data, 0, length);
144+
}
145+
dataOut.flush();
146+
}
147+
148+
private String formatDate(int seconds) {
149+
SimpleDateFormat simpleDate = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
150+
return simpleDate.format(seconds);
151+
}
152+
153+
private byte[] readerData(File file, int lentgh) throws IOException {
154+
FileInputStream input = null;
155+
byte[] data = new byte[lentgh];
156+
157+
try {
158+
input = new FileInputStream(file);
159+
input.read(data);
160+
} finally {
161+
if (input != null) {
162+
input.close();
163+
}
164+
}
165+
return data;
166+
}
167+
168+
private String getType(File fileRequest) {
169+
String requested = fileRequest.getName();
170+
String type = "application/octet-stream";
171+
172+
if (requested.endsWith(".htm") || requested.endsWith(".html")) {
173+
type = "text/html";
174+
}
175+
if (requested.endsWith(".log") || requested.endsWith(".txt")) {
176+
type = "text/plain";
177+
}
178+
if (requested.endsWith(".gif")) {
179+
type = "image/gif";
180+
}
181+
if (requested.endsWith(".png")) {
182+
type = "image/png";
183+
}
184+
return type;
185+
}
186+
187+
}

0 commit comments

Comments
 (0)