summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/test/ChunkReader.java
blob: a550a013a3b649dfd38a5db0f773281e41a158e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.test;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
public class ChunkReader {

    private static final Pattern CONTENT_LENGTH = Pattern.compile(".+^content-length: (\\d+)$.*",
                                                                  Pattern.CASE_INSENSITIVE |
                                                                  Pattern.MULTILINE |
                                                                  Pattern.DOTALL);
    private static final Pattern CHUNKED_ENCODING = Pattern.compile(".+^transfer-encoding: chunked$.*",
                                                                    Pattern.CASE_INSENSITIVE |
                                                                    Pattern.MULTILINE |
                                                                    Pattern.DOTALL);
    private final InputStream in;
    private StringBuilder reading = new StringBuilder();
    private boolean readingHeader = true;

    public ChunkReader(InputStream in) {
        this.in = in;
    }

    public boolean isEndOfContent() throws IOException {
        if (in.available() != 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(in.available()).append(": ");
            for(int c = in.read(); c != -1; c = in.read()) {
                sb.append('\'');
                sb.append(c);
                sb.append("' ");
            }
            throw new IllegalStateException("This is not the end '" + sb.toString());
        }
        return in.available() == 0;
    }

    public String readChunk() throws IOException {
        while (true) {
            String ret = removeNextChunk();
            if (ret != null) {
                return ret;
            }
            readFromStream();
        }
    }

    private String readContent(int length) throws IOException {
        while (reading.length() < length) {
            readFromStream();
        }
        return splitReadBuffer(length);
    }

    private void readFromStream() throws IOException {
        byte[] buf = new byte[4096];
        try {
            while (!Thread.currentThread().isInterrupted()) {
                int len = in.read(buf, 0, buf.length);
                if (len < 0) {
                    throw new IOException("Socket is closed.");
                }
                if (len > 0) {
                    reading.append(StandardCharsets.UTF_8.decode(ByteBuffer.wrap(buf, 0, len)));
                    break;
                }
                Thread.sleep(10);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    private String removeNextChunk() throws IOException {
        if (readingHeader) {
            int pos = reading.indexOf("\r\n\r\n");
            if (pos < 0) {
                return null;
            }
            String ret = splitReadBuffer(pos + 4);
            Matcher m = CONTENT_LENGTH.matcher(ret);
            if (m.matches()) {
                ret += readContent(Integer.valueOf(m.group(1)));
            }
            readingHeader = !CHUNKED_ENCODING.matcher(ret).matches();
            return ret;
        } else if (reading.indexOf("0\r\n") == 0) {
            int pos = reading.indexOf("\r\n\r\n", 1);
            if (pos < 0) {
                return null;
            }
            readingHeader = true;
            return splitReadBuffer(pos + 4);
        } else {
            int pos = reading.indexOf("\r\n");
            if (pos < 0) {
                return null;
            }
            pos = reading.indexOf("\r\n", pos + 2);
            if (pos < 0) {
                return null;
            }
            return splitReadBuffer(pos + 2);
        }
    }

    private String splitReadBuffer(int pos) {
        String ret = reading.substring(0, pos);
        if (pos < reading.length()) {
            reading = new StringBuilder(reading.substring(pos));
        } else {
            reading = new StringBuilder();
        }
        return ret;
    }
}