Skip to content

Instantly share code, notes, and snippets.

View pumpkincouture's full-sized avatar

Sylwia Bridges pumpkincouture

View GitHub Profile
public class RequestParser {
static final int FIRST_ELEMENT = 0;
static final int SECOND_ELEMENT = 1;
static final int ELEMENTS_IN_REQUEST_LINE = 3;
static final String LINE_BREAK = "\\n";
static final String EMPTY_SPACE = " ";
static final String COLON = ": ";
static final String AFTER_COLON = ":\\s";
static final String EQUAL_SIGN = "=";
public HashMap<String, String> getHeaders() {
return createTable(getMatchingStrings();
}
private List<String> getMatchingStrings() {
List<String> headersStrings = new ArrayList<>();
for (String string : splitRequest) {
if (string.contains(": ")) {
headersStrings.add(string);
}
# Factory class
public class HandlerFactory {
private Request request;
private ResponseCodeBuilder responseCodeBuilder;
public HandlerFactory(Request request) {
this.request = request;
responseCodeBuilder = new ResponseCodeBuilder(request);
}
public class ServerMain {
static public void main (String args[]) throws IOException {
try {
ServerSocket server = new ServerSocket(5000);
while (true) {
Socket clientSocket = server.accept();
# Params sent in the body of the request
POST / HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
a=3&b=2&c=1
# Params sent in the URL
script : http browser
set host : localhost
set port : 5000
get : /form
ensure : response code equals 200
reject : body has content data=fatcat
set data : data=fatcat
post : /form
# Request from Client
GET /path/file.html HTTP/1.0
From: [email protected]
User-Agent: HTTPTool/1.0
[blank line here]
# Response from Server
HTTP/1.0 200 OK
GET / HTTP/1.1
Host: localhost:5000
Connection: keep-alive
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.104 Safari/537.36
Accept: */*
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
Cookie: textwrapon=false; wysiwyg=textarea
public class EchoServer {
public static void main(String[] args) throws IOException {
try {
ServerSocket serverSocket = new ServerSocket(5000);
while (true) {
Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
public class EchoClient {
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.err.println("Usage: java EchoClient <host name> <port number>");
System.exit(1);
}
String hostName = args[0];
int portNumber = Integer.parseInt(args[1]);