Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. package com.pablo67340.textures;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.DataOutputStream;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.InputStreamReader;
  8. import java.net.Socket;
  9. import java.util.StringTokenizer;
  10.  
  11.  
  12.  
  13. public class WebHandler extends Thread
  14. {
  15. private Socket client;
  16. private BufferedReader inFromClient = null;
  17. private DataOutputStream outToClient = null;
  18. private Webserver ws = null;
  19.  
  20. public WebHandler(Socket c, Webserver w) { this.client = c;
  21. this.ws = w; }
  22.  
  23. public void markDone(String username) {
  24. boolean done = false;
  25. while (!done)
  26. if (!this.ws.ft.checklocked) {
  27. this.ws.ft.checklocked = true;
  28. if ((this.ws.ft.checks.containsKey(username)) &&
  29. (!((Boolean)this.ws.ft.checks.get(username)).booleanValue())) {
  30. this.ws.ft.checks.put(username, Boolean.valueOf(true));
  31. }
  32.  
  33. this.ws.ft.checklocked = false;
  34. done = true;
  35. }
  36. }
  37.  
  38. public void run() {
  39. try {
  40. this.inFromClient = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
  41. this.outToClient = new DataOutputStream(this.client.getOutputStream());
  42.  
  43. String requestString = this.inFromClient.readLine();
  44. String headerLine = requestString;
  45. StringTokenizer tokenizer = new StringTokenizer(headerLine);
  46. String httpMethod = tokenizer.nextToken();
  47. String httpQueryString = tokenizer.nextToken();
  48. boolean mc = false;
  49. while (this.inFromClient.ready())
  50. {
  51. requestString = this.inFromClient.readLine();
  52. String[] v = requestString.split(": ");
  53. if (v.length > 1) {
  54. String field = v[0];
  55. String value = v[1];
  56. if (field.equals("X-Minecraft-Username")) {
  57. markDone(value);
  58. mc = true;
  59. }
  60. }
  61.  
  62. }
  63.  
  64. if (httpMethod.equals("GET")) {
  65. if (httpQueryString.equals("/"))
  66. {
  67. sendResponse(200, "MC Texture Pack Server - Created by pablo67340", false);
  68. }
  69. if (httpQueryString.equals("/ping"))
  70. {
  71. sendResponse(222, "MCTP Server v" + this.ws.ft.getDescription().getVersion(), false);
  72. }
  73. if (!mc) {
  74. sendResponse(200, "This server is intended for Minecraft Texture Pack Requests Only.", false);
  75. return;
  76. }
  77. if (httpQueryString.equals("/halo.zip")) {
  78. String fileName = this.ws.ft.filename;
  79. if (new File(fileName).isFile()) {
  80. sendResponse(200, fileName, true);
  81. }
  82. else
  83. sendResponse(404, "", false);
  84. }
  85. }
  86. else {
  87. sendResponse(404, "<b>The Requested resource not found ....Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/</b>",
  88. false);
  89. }
  90. } catch (Exception localException) {
  91. }
  92. }
  93.  
  94. public void sendResponse(int statusCode, String responseString, boolean isFile) throws Exception {
  95. String statusLine = null;
  96. String serverdetails = "Server: Java HTTPServer";
  97. String contentLengthLine = null;
  98. String fileName = null;
  99. String contentTypeLine = "Content-Type: text/html\r\n";
  100. FileInputStream fin = null;
  101. statusLine = "HTTP/1.1 200 OK\r\n";
  102. switch (statusCode) {
  103. case 200:
  104. statusLine = "HTTP/1.1 200 OK\r\n";
  105. break;
  106. case 500:
  107. statusLine = "HTTP/1.1 500 Internal Server Error\r\n";
  108. break;
  109. case 222:
  110. statusLine = "HTTP/1.1 222 Ping Response\r\n";
  111. break;
  112. default:
  113. statusLine = "HTTP/1.1 404 Not Found\r\n";
  114. }
  115. if (isFile) {
  116. fileName = responseString;
  117. fin = new FileInputStream(fileName);
  118. contentLengthLine = "Content-Length: " + Integer.toString(fin.available()) + "\r\n";
  119. if ((!fileName.endsWith(".htm")) && (!fileName.endsWith(".html")))
  120. contentTypeLine = "Content-Type: application/zip\r\n";
  121. }
  122. else {
  123. contentLengthLine = "Content-Length: " + responseString.length() + "\r\n";
  124. }
  125. if (!this.client.isClosed()) {
  126. this.outToClient.writeBytes(statusLine);
  127. this.outToClient.writeBytes(serverdetails);
  128. this.outToClient.writeBytes(contentTypeLine);
  129. this.outToClient.writeBytes(contentLengthLine);
  130. this.outToClient.writeBytes("Connection: close\r\n");
  131. this.outToClient.writeBytes("\r\n");
  132.  
  133. if (isFile) {
  134. byte[] buffer = new byte[1024];
  135. @SuppressWarnings("unused")
  136. int bytesRead;
  137. while ((bytesRead = fin.read(buffer)) != -1)
  138. {
  139. @SuppressWarnings("unused")
  140. int bytesRead1 = 0;
  141. this.outToClient.write(buffer, 0, bytesRead1 = 0);
  142. }
  143. fin.close();
  144. }
  145. else {
  146. this.outToClient.writeBytes(responseString);
  147. }
  148. this.outToClient.close();
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement