Projekt

Obecné

Profil

Stáhnout (5.11 KB) Statistiky
| Větev: | Tag: | Revize:
1
import java.io.BufferedOutputStream;
2
import java.io.BufferedReader;
3
import java.io.BufferedWriter;
4
import java.io.ByteArrayOutputStream;
5
import java.io.IOException;
6
import java.io.InputStreamReader;
7
import java.io.OutputStreamWriter;
8
import java.net.ServerSocket;
9
import java.net.Socket;
10
import java.util.Calendar;
11
import java.util.Locale;
12
import java.util.TimeZone;
13
import java.util.zip.GZIPOutputStream;
14

    
15
// TODO
16
public class BadHttpServer {
17
    private static int request = 0, connection = 0;
18
    private static final BadHttpServer SINGLETON = new BadHttpServer();
19
    private ServerSocket serverSocket;
20
    private String htmlString, formattedDate;
21
    private byte[] compressedHTML;
22

    
23
    public String getURL() {
24
        return "http://localhost:60000";
25
    }
26

    
27
    synchronized byte[] getCompressedHTML() {
28
        return compressedHTML;
29
    }
30

    
31
    synchronized void setCompressedHTML(String string) {
32
        try (ByteArrayOutputStream baos = new ByteArrayOutputStream(string.length() / 2);
33
                GZIPOutputStream gos = new GZIPOutputStream(baos);) {
34
            gos.write(string.getBytes("utf-8"));
35
            gos.close();
36
            this.compressedHTML = baos.toByteArray();
37
            formattedDate = String.format(Locale.US, "%1$ta, %1$td %1$tb %1$tY %1$tH:%1$tM:%1$tS %1$tZ",
38
                    Calendar.getInstance(TimeZone.getTimeZone("GMT")));
39
        } catch (IOException e) {
40
            e.printStackTrace();
41
        }
42
    }
43

    
44
    private synchronized String getFormattedDate() {
45
        return formattedDate;
46
    }
47

    
48
    private BadHttpServer() {
49
        try {
50
            serverSocket = new ServerSocket(60000);
51
        } catch (IOException e) {
52
            e.printStackTrace();
53
        }
54
        new Thread() {
55
            {
56
                setDaemon(true);
57
                setName("serverSocketThread");
58
            }
59

    
60
            public void run() {
61
                while (true) {
62
                    try {
63
                        new Thread(acceptAndSendHTML(serverSocket.accept())) {
64
                            {
65
                                setDaemon(true);
66
                                setName("acceptThread" + connection++);
67
                            }
68
                        }.start();
69
                    } catch (IOException e) {
70
                        // e.printStackTrace();
71
                    }
72
                }
73
            };
74
        }.start();
75
    }
76

    
77
    protected Runnable acceptAndSendHTML(final Socket socket) {
78
        return new Runnable() {
79
            @Override
80
            public void run() { 
81
                try (BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "ASCII"));
82
                        BufferedWriter asciiWriter = new BufferedWriter(
83
                                new OutputStreamWriter(socket.getOutputStream(), "ASCII"));
84
                        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream(), 1000)) {
85
                    while (!socket.isClosed()) {
86
                        int request = BadHttpServer.request++;
87
                        // READING
88
                        StringBuilder stringBuilder = new StringBuilder();
89
                        stringBuilder.append(br.readLine() + "\r\n");
90
                        for (String line = ""; !(line = br.readLine()).isEmpty(); stringBuilder.append(line + "\r\n"))
91
                            ;
92
                        // WRITING
93
                        byte[] compressedHTML = getCompressedHTML();
94
                        asciiWriter.write("HTTP/1.1 100 Continue\r\n");
95
                        asciiWriter.write("\r\n");
96
                        asciiWriter.write("HTTP/1.1 200 OK\r\n");
97
                        asciiWriter.write("Date: " + getFormattedDate() + "\r\n");
98
                        asciiWriter.write("Content-Type: text/html; charset=utf-8\r\n");
99
                        asciiWriter.write("Content-Encoding: gzip\r\n");
100
                        asciiWriter.write("Content-Charset: utf-8\r\n");
101
                        asciiWriter.write("Content-Length: " + compressedHTML.length + "\r\n");
102
                        asciiWriter.write("\r\n");
103
                        asciiWriter.flush();
104
                        for (int writtenOverall = 0, writtenThisIteration = 0; writtenOverall < compressedHTML.length; writtenOverall += writtenThisIteration) {
105
                            bos.write(compressedHTML, writtenOverall,
106
                                    writtenThisIteration = Math.min(compressedHTML.length - writtenOverall, 1000));
107
                            Thread.sleep(10); // Bandwidth throttling
108
                        }
109
                        bos.flush();
110
                    }
111
                } catch (IOException e) {
112
                    e.printStackTrace();
113
                } catch (InterruptedException e) {
114
                    e.printStackTrace();
115
                }
116
            }
117
        };
118
    }
119

    
120
    public static BadHttpServer getIntance() {
121
        return SINGLETON;
122
    }
123

    
124
    public synchronized void setHtmlString(String htmlString) {
125
        this.htmlString = htmlString;
126
        setCompressedHTML(htmlString);
127
    }
128

    
129
    synchronized String getHtmlString() {
130
        return htmlString;
131
    }
132

    
133
}
(1-1/7)