Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.InputStreamReader;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.util.Date;
  10. import java.util.StringTokenizer;
  11.  
  12. public class HTTPServer {
  13.  
  14. public static final int MIMETYPE_HTML = 0;
  15. public static final int MIMETYPE_JPEG = 2;
  16. public static final int MIMETYPE_ZIP = 3;
  17. public static final int MIMETYPE_GIF = 4;
  18. public static final int MIMETYPE_PNG = 5;
  19. public static final int MIMETYPE_MP3 = 6;
  20. public static final int MIMETYPE_ASF = 7;
  21. public static final String serverVersion = "Custom Web server v.0.1";
  22. public static String wwwRoot;
  23.  
  24. HTTPServerConsole console = null;
  25. ServerSocket serverSocket = null;
  26. public HTTPServer(int port, HTTPServerConsole console, String wwwRoot) {
  27. this.wwwRoot = wwwRoot;
  28. this.console = console;
  29. try {
  30. console.logInfo("Trying to bind server to port " + port);
  31. serverSocket = new ServerSocket(port);
  32. console.logInfo("Server bound to port: " + port);
  33. console.logInfo("Server started normally");
  34.  
  35. } catch(Exception e) {
  36. console.logError(e.toString());
  37. }
  38.  
  39. }
  40.  
  41.  
  42. public void run() {
  43.  
  44. while(true) {
  45. try {
  46. console.logInfo("Waiting for connections");
  47. Socket clientConnection = serverSocket.accept();
  48.  
  49. console.logInfo("Incoming connection from " + clientConnection.getInetAddress().getHostName());
  50. BufferedReader bis = new BufferedReader(new InputStreamReader(clientConnection.getInputStream()));
  51. DataOutputStream dos = new DataOutputStream(clientConnection.getOutputStream());
  52. protocolHandler(bis, dos);
  53.  
  54. } catch(Exception e) {
  55. console.logError(e.toString());
  56. }
  57. }
  58. }
  59.  
  60.  
  61.  
  62. private void protocolHandler(BufferedReader input, DataOutputStream output) {
  63. try {
  64. String request = input.readLine();
  65. System.out.println(request);
  66. //processing the request
  67.  
  68. if (request == null) {
  69. //null request is not valid
  70. output.writeBytes(constructHTTPHeader(403, MIMETYPE_HTML));
  71. output.close();
  72. console.logError("Request is null");
  73. return;
  74. }
  75.  
  76. //process the request
  77. StringTokenizer strTok = new StringTokenizer(request, " ");
  78. String reqMethod = strTok.nextToken();
  79. String resourceName = strTok.nextToken().substring(1); //remove slash
  80.  
  81. if (reqMethod.equals("HEAD") || reqMethod.equals("GET") || reqMethod.equals("OPTIONS")) {
  82. try {
  83. console.logInfo("Client requested resource:" + new File(wwwRoot + resourceName).getAbsolutePath());
  84. FileInputStream fis = new FileInputStream(wwwRoot + resourceName);
  85. //check the type of resource
  86. if (supportedResource(resourceName)) {
  87. output.writeBytes(constructHTTPHeader(200, getMimeType(wwwRoot + resourceName)));
  88. if (reqMethod.equals("GET")){
  89. while (true) { //write the resource to the stream
  90. int b = fis.read();
  91. if (b == -1) {
  92. break; // end of file
  93. }
  94. output.write(b);}
  95. }
  96. output.close();
  97. fis.close();
  98. return;
  99. } else { //media type not supported
  100. output.writeBytes(constructHTTPHeader(403, MIMETYPE_HTML)); //access to this resource is forbidden
  101. output.close();
  102. console.logError("Resource type not supported:" + resourceName);
  103. return;
  104. }
  105.  
  106.  
  107. } catch(FileNotFoundException fnf) {
  108. output.writeBytes(constructHTTPHeader(404, MIMETYPE_HTML));
  109. output.close();
  110. console.logError("Resource not found");
  111. return;
  112. } catch(SecurityException se) {
  113. output.writeBytes(constructHTTPHeader(403, MIMETYPE_HTML));
  114. console.logError("Access is denied");
  115. output.close();
  116. return;
  117. }catch(Exception e) {
  118. output.writeBytes(constructHTTPHeader(500, MIMETYPE_HTML));
  119. output.close();
  120. return;
  121. }
  122. } else {
  123. output.writeBytes(constructHTTPHeader(501, 0)); //not implemented
  124. output.close();
  125. return;
  126. }
  127.  
  128.  
  129.  
  130. } catch(Exception e) {
  131. console.logError("ERROR" + e.toString());
  132. }
  133. }
  134.  
  135.  
  136. private boolean supportedResource(String resource) {
  137. String cResource = resource.toUpperCase();
  138.  
  139. if(cResource.endsWith(".JPEG") ||
  140. cResource.endsWith(".JPG") ||
  141. cResource.endsWith(".PNG") ||
  142. cResource.endsWith(".GIF") ||
  143. cResource.endsWith(".ZIP") ||
  144. cResource.endsWith("MP3") ||
  145. cResource.endsWith("WMV") ||
  146. cResource.endsWith(".HTML")) {
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. }
  152.  
  153. private int getMimeType(String resource) {
  154. String cResource = resource.toUpperCase();
  155.  
  156. if (cResource.endsWith(".JPG") || cResource.endsWith(".JPEG"))
  157. return this.MIMETYPE_JPEG;
  158. else if (cResource.endsWith(".GIF") )
  159. return this.MIMETYPE_GIF;
  160. else if (cResource.endsWith(".PNG"))
  161. return this.MIMETYPE_PNG;
  162. else if (cResource.endsWith(".ZIP"))
  163. return this.MIMETYPE_ZIP;
  164. else if (cResource.endsWith(".MP3"))
  165. return this.MIMETYPE_MP3;
  166. else if (cResource.endsWith(".WMV"))
  167. return this.MIMETYPE_ASF;
  168. else //html
  169. return this.MIMETYPE_HTML;
  170. }
  171.  
  172. private String constructHTTPHeader(int replyCode, int mimeType) {
  173. File file = new File(wwwRoot);
  174. String replyHeader = "HTTP/1.1 ";
  175.  
  176. switch(replyCode) {
  177. case 200:
  178. replyHeader += "200 OK";
  179. break;
  180. case 400:
  181. replyHeader += "400 Bad Request";
  182. break;
  183. case 404:
  184. replyHeader += "404 Not Found";
  185. break;
  186. case 403:
  187. replyHeader += "403 Forbidden";
  188. break;
  189. case 501:
  190. replyHeader += "501 NOT Implemented";
  191. break;
  192. case 500:
  193. replyHeader += "500 Internal Server error";
  194. break;
  195. }
  196. replyHeader += "\r\n";
  197. Date d = new Date();
  198. replyHeader += "Date:" + d.toString() + "\r\n";
  199. //replyHeader += "Server:" + serverVersion + "\r\n";
  200.  
  201. replyHeader += "Allow: GET, HEAD, OPTIONS\r\n";//It usually appears on a 405 error message, but as this message isn't handled above, the Allow option appears every time
  202. replyHeader += "Connection: close\r\n";
  203. replyHeader += "Content-Length: " + file.length() + "\r\n";
  204. replyHeader += "Last-Modified: " + file.lastModified() + "\r\n";
  205.  
  206. switch(mimeType) {
  207. case MIMETYPE_JPEG:
  208. replyHeader += "Content-Type: image/jpeg\r\n";
  209. break;
  210. case MIMETYPE_PNG:
  211. replyHeader += "Content-Type: image/png\r\n";
  212. break;
  213. case MIMETYPE_GIF:
  214. replyHeader += "Content-Type: image/GIF\r\n";
  215. break;
  216. case MIMETYPE_ZIP:
  217. replyHeader += "Content-Type: application/zip\r\n";
  218. break;
  219. case MIMETYPE_MP3:
  220. replyHeader += "Content-Type: audio/mp3\r\n";
  221. break;
  222. case MIMETYPE_ASF:
  223. replyHeader += "Content-Type: video/x-ms-asf\r\n";
  224. break;
  225. default:
  226. replyHeader += "Content-Type: text/html\r\n";
  227. break;
  228. }
  229.  
  230. replyHeader += "\r\n";
  231. //System.out.println(replyHeader);
  232. return replyHeader;
  233. }
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement