Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pablo67340.textures;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import java.util.StringTokenizer;
- public class WebHandler extends Thread
- {
- private Socket client;
- private BufferedReader inFromClient = null;
- private DataOutputStream outToClient = null;
- private Webserver ws = null;
- public WebHandler(Socket c, Webserver w) { this.client = c;
- this.ws = w; }
- public void markDone(String username) {
- boolean done = false;
- while (!done)
- if (!this.ws.ft.checklocked) {
- this.ws.ft.checklocked = true;
- if ((this.ws.ft.checks.containsKey(username)) &&
- (!((Boolean)this.ws.ft.checks.get(username)).booleanValue())) {
- this.ws.ft.checks.put(username, Boolean.valueOf(true));
- }
- this.ws.ft.checklocked = false;
- done = true;
- }
- }
- public void run() {
- try {
- this.inFromClient = new BufferedReader(new InputStreamReader(this.client.getInputStream()));
- this.outToClient = new DataOutputStream(this.client.getOutputStream());
- String requestString = this.inFromClient.readLine();
- String headerLine = requestString;
- StringTokenizer tokenizer = new StringTokenizer(headerLine);
- String httpMethod = tokenizer.nextToken();
- String httpQueryString = tokenizer.nextToken();
- boolean mc = false;
- while (this.inFromClient.ready())
- {
- requestString = this.inFromClient.readLine();
- String[] v = requestString.split(": ");
- if (v.length > 1) {
- String field = v[0];
- String value = v[1];
- if (field.equals("X-Minecraft-Username")) {
- markDone(value);
- mc = true;
- }
- }
- }
- if (httpMethod.equals("GET")) {
- if (httpQueryString.equals("/"))
- {
- sendResponse(200, "MC Texture Pack Server - Created by pablo67340", false);
- }
- if (httpQueryString.equals("/ping"))
- {
- sendResponse(222, "MCTP Server v" + this.ws.ft.getDescription().getVersion(), false);
- }
- if (!mc) {
- sendResponse(200, "This server is intended for Minecraft Texture Pack Requests Only.", false);
- return;
- }
- if (httpQueryString.equals("/halo.zip")) {
- String fileName = this.ws.ft.filename;
- if (new File(fileName).isFile()) {
- sendResponse(200, fileName, true);
- }
- else
- sendResponse(404, "", false);
- }
- }
- else {
- sendResponse(404, "<b>The Requested resource not found ....Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/</b>",
- false);
- }
- } catch (Exception localException) {
- }
- }
- public void sendResponse(int statusCode, String responseString, boolean isFile) throws Exception {
- String statusLine = null;
- String serverdetails = "Server: Java HTTPServer";
- String contentLengthLine = null;
- String fileName = null;
- String contentTypeLine = "Content-Type: text/html\r\n";
- FileInputStream fin = null;
- statusLine = "HTTP/1.1 200 OK\r\n";
- switch (statusCode) {
- case 200:
- statusLine = "HTTP/1.1 200 OK\r\n";
- break;
- case 500:
- statusLine = "HTTP/1.1 500 Internal Server Error\r\n";
- break;
- case 222:
- statusLine = "HTTP/1.1 222 Ping Response\r\n";
- break;
- default:
- statusLine = "HTTP/1.1 404 Not Found\r\n";
- }
- if (isFile) {
- fileName = responseString;
- fin = new FileInputStream(fileName);
- contentLengthLine = "Content-Length: " + Integer.toString(fin.available()) + "\r\n";
- if ((!fileName.endsWith(".htm")) && (!fileName.endsWith(".html")))
- contentTypeLine = "Content-Type: application/zip\r\n";
- }
- else {
- contentLengthLine = "Content-Length: " + responseString.length() + "\r\n";
- }
- if (!this.client.isClosed()) {
- this.outToClient.writeBytes(statusLine);
- this.outToClient.writeBytes(serverdetails);
- this.outToClient.writeBytes(contentTypeLine);
- this.outToClient.writeBytes(contentLengthLine);
- this.outToClient.writeBytes("Connection: close\r\n");
- this.outToClient.writeBytes("\r\n");
- if (isFile) {
- byte[] buffer = new byte[1024];
- @SuppressWarnings("unused")
- int bytesRead;
- while ((bytesRead = fin.read(buffer)) != -1)
- {
- @SuppressWarnings("unused")
- int bytesRead1 = 0;
- this.outToClient.write(buffer, 0, bytesRead1 = 0);
- }
- fin.close();
- }
- else {
- this.outToClient.writeBytes(responseString);
- }
- this.outToClient.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement