Skip to content

Commit 5725e6a

Browse files
committed
add more documentation
1 parent c4de2bd commit 5725e6a

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

HttpRequest.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
import java.text.SimpleDateFormat;
55
import static SSHelpers.Util.*;
66

7+
/**
8+
* This class contains the needed information (host, port number and all Http headers) for a typical Http Request.
9+
*
10+
* @author Modified by: Sebastian Schmidt
11+
* @version 1.0 2016-10-18
12+
* @since 1.0
13+
*/
714
public class HttpRequest {
815
/** Help variables */
916
final static String CRLF = "\r\n";
@@ -18,7 +25,14 @@ public class HttpRequest {
1825
private int port;
1926
private int error;
2027

21-
/** Create HttpRequest by reading it from the client socket */
28+
/**
29+
* Creates a HttpRequest by reading it from the client socket.
30+
* The constructor attempts to parse the content form the client.
31+
* If there is a error in parsing the client will receive a
32+
* 400 (Bad request) error indicating that the request was malformed.
33+
* @param from The content stream from a socket connection.
34+
* @return A new HttpRequest.
35+
*/
2236
public HttpRequest(BufferedReader from) {
2337
error = 0;
2438
String firstLine = "";
@@ -57,17 +71,25 @@ public HttpRequest(BufferedReader from) {
5771
}
5872

5973
// TODO: get POST parameters
60-
//
61-
//
62-
} catch (IOException e) {
74+
75+
} catch (Exception e) {
6376
error = 400;
64-
System.out.println("Error reading from socket: " + e);
77+
System.out.println("Error reading from client socket: " + e);
6578
return;
6679
}
6780
System.out.println("Host to contact is: " + host + " at port " + port);
6881
}
6982

70-
// constructor
83+
/**
84+
* Constructs a HttpRequest given the required variables
85+
* @param method The http method E.g GET or POST
86+
* @param url The location of the resource E.g. /example.html
87+
* @param version The Http protocol version, usually it is HTTP/1.1
88+
* @param headers Any optional http headers. If you don't want to add any use an empty string ("").
89+
* @param host The hostname of the server where the request is send to E.g. example.com
90+
* @param port The port number used for the webs server E.g. 80
91+
* @return A HttpRequet object
92+
*/
7193
public HttpRequest(String method, String url, String version, String headers, String host, int port) {
7294
this.method = method;
7395
this.URL = url;
@@ -78,7 +100,11 @@ public HttpRequest(String method, String url, String version, String headers, St
78100
this.error = 0;
79101
}
80102

81-
// copy constructor
103+
/**
104+
* Copy constructor
105+
* @param other An existing HttpRequest to create the copy from
106+
* @return A deep copy of the 'other' HttpRequest
107+
*/
82108
public HttpRequest(HttpRequest other) {
83109
this.method = other.method;
84110
this.URL = other.URL;

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ docs: *.java
1717
$(JDOC) $^ -d docs \
1818
-windowtitle $(DOCTITLE) \
1919
-doctitle $(DOCTITLE) \
20-
-subpackages SSHelpers
20+
-subpackages SSHelpers \
21+
-version \
22+
-author
2123

2224
docs-clean:
2325
$(RM) docs/*

SSHelpers/Util.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
import java.util.TimeZone;
55
import java.text.ParseException;
66
import java.text.SimpleDateFormat;
7-
7+
/**
8+
* A utility class containing small helper methods.
9+
* These methods can be used my importing the class <code>import static SSHelpers.Util.*;</code>
10+
*
11+
* @author Sebastian Schmidt
12+
* @version 1.0 2016-10-18
13+
* @since 1.0
14+
*/
815
public class Util {
16+
917
/**
1018
* From a url string get the host-name
1119
* @param url The Url E.g. http://example.com/foo
@@ -88,6 +96,12 @@ public static String getLocation(String url) {
8896
return url.substring(end);
8997
}
9098

99+
/**
100+
* Converts a string to a Java date object given the correct format.
101+
* @param dateStr The string that is parsed as a date.
102+
* @param format The format as defined in the SimpleDateFormat <a href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html">https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html</a>.
103+
* @return Returns a new date object. On failure it will return null.
104+
*/
91105
public static Date toDate(String dateStr, String format) {
92106
try {
93107
SimpleDateFormat inFormat = new SimpleDateFormat(format);
@@ -98,18 +112,33 @@ public static Date toDate(String dateStr, String format) {
98112
}
99113
return null;
100114
}
101-
115+
/**
116+
* Converts a string to a Java date object using "E, dd MMM yyyy HH:mm:ss z" (Mon, 13 Jun 2016 21:06:31 GMT) as the format.
117+
* @param dateStr The string that is parsed as a date.
118+
* @return Returns a new date object. On failure it will return null.
119+
*/
102120
public static Date toDate(String dateStr) {
103-
String format = "E, dd MMM yyyy HH:mm:ss z"; // Mon, 13 Jun 2016 21:06:31 GMT
121+
String format = "E, dd MMM yyyy HH:mm:ss z";
104122
return Util.toDate(dateStr, format);
105123
}
106124

125+
/**
126+
* Converts a Java string object to a string with the given format.
127+
* @param date The date to format.
128+
* @param format The format as defined in the SimpleDateFormat <a href="https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html">https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html</a>.
129+
* @return The date as a string.
130+
*/
107131
public static String dateToString(Date date, String format) {
108132
SimpleDateFormat dateFormat = new SimpleDateFormat (format);
109133
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
110134
return dateFormat.format(date);
111135
}
112136

137+
/**
138+
* Converts a Java string object to a string using "E, dd MMM yyyy HH:mm:ss z" (Mon, 13 Jun 2016 21:06:31 GMT) as the format.
139+
* @param date The date to format.
140+
* @return The date as a string.
141+
*/
113142
public static String dateToString(Date date) {
114143
return Util.dateToString(date, "E, dd MMM yyyy HH:mm:ss z");
115144
}

0 commit comments

Comments
 (0)