Last active
January 26, 2019 13:26
In request object we are going to process the HTTP request coming from client browsers. In Request.java class we are going to process the request and see the URLs browsers are requesting are valid or not. In request class we are read the request from socket request stream and striping up the requested page name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @Author Anindya Bandopadhyay | |
* | |
* SimpleHttpWebServer\andy.blog.web.server.Request.java | |
* | |
* @Created on Sep 14, 2014 6:55:52 PM | |
*/ | |
package andy.blog.web.server; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import andy.blog.constant.ServerConstant; | |
public class Request { | |
public String getUri(InputStream inputStream) { | |
byte buffer[] = new byte[ServerConstant.BUFFER_SIZE]; | |
StringBuffer inputBuffer = new StringBuffer(ServerConstant.BUFFER_SIZE); | |
int byteRequest; | |
try { | |
byteRequest = inputStream.read(buffer); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
byteRequest = -1; | |
} | |
for (int index = 0; index < byteRequest; index++) { | |
inputBuffer.append((char) buffer[index]); | |
} | |
String request = inputBuffer.toString(); | |
System.out.println(request); | |
return parseUri(request); | |
} | |
private String parseUri(String requestString) { | |
// GET /index.html HTTP/1.1 | |
// strip out '/index.html' from request string | |
String[] resource = requestString.split(" "); | |
return resource[1]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment