Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6e182a57c476b56a2df3cfc585300059dd01a7af (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
//  ========================================================================
//  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.servlets;

import static org.hamcrest.Matchers.not;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.log.StdErrLog;
import org.junit.Assert;

public class PipelineHelper
{
    private static final Logger LOG = Log.getLogger(PipelineHelper.class);
    private URI uri;
    private SocketAddress endpoint;
    private Socket socket;
    private OutputStream outputStream;
    private InputStream inputStream;
    private String encodingHeader;

    public PipelineHelper(URI uri, String encodingHeader)
    {
        this.uri = uri;
        this.endpoint = new InetSocketAddress(uri.getHost(),uri.getPort());
        this.encodingHeader = encodingHeader;
    }

    /**
     * Open the Socket to the destination endpoint and
     *
     * @return the open java Socket.
     * @throws IOException
     */
    public Socket connect() throws IOException
    {
        LOG.info("Connecting to endpoint: " + endpoint);
        socket = new Socket();
        socket.setTcpNoDelay(true);
        socket.connect(endpoint,1000);

        outputStream = socket.getOutputStream();
        inputStream = socket.getInputStream();

        return socket;
    }

    /**
     * Issue a HTTP/1.1 GET request with Connection:keep-alive set.
     *
     * @param path
     *            the path to GET
     * @param acceptGzipped
     *            to turn on acceptance of GZIP compressed responses
     * @throws IOException
     */
    public void issueGET(String path, boolean acceptGzipped, boolean close) throws IOException
    {
        LOG.debug("Issuing GET on " + path);
        StringBuilder req = new StringBuilder();
        req.append("GET ").append(uri.resolve(path).getPath()).append(" HTTP/1.1\r\n");
        req.append("Host: ").append(uri.getHost()).append(":").append(uri.getPort()).append("\r\n");
        req.append("User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3\r\n");
        req.append("Accept: */*\r\n");
        req.append("Referer: http://mycompany.com/index.html\r\n");
        req.append("Accept-Language: en-us\r\n");
        if (acceptGzipped)
        {
            req.append("Accept-Encoding: " + encodingHeader + "\r\n");
        }
        req.append("Cookie: JSESSIONID=spqx8v8szylt1336t96vc6mw0\r\n");
        if ( close )
        {
            req.append("Connection: close\r\n");
        }
        else
        {
            req.append("Connection: keep-alive\r\n");
        }

        req.append("\r\n");

        LOG.debug("Request:" + req);

        // Send HTTP GET Request
        byte buf[] = req.toString().getBytes();
        outputStream.write(buf,0,buf.length);
        outputStream.flush();
    }

    public String readResponseHeader() throws IOException
    {
        // Read Response Header
        socket.setSoTimeout(10000);

        LOG.debug("Reading http header");
        StringBuilder response = new StringBuilder();
        boolean foundEnd = false;
        String line;
        while (!foundEnd)
        {
            line = readLine();
            // System.out.printf("RESP: \"%s\"%n",line);
            if (line.length() == 0)
            {
                foundEnd = true;
                LOG.debug("Got full http response header");
            }
            else
            {
                response.append(line).append("\r\n");
            }
        }

        return response.toString();
    }

    public String readLine() throws IOException
    {
        StringBuilder line = new StringBuilder();
        boolean foundCR = false;
        boolean foundLF = false;
        int b;
        while (!(foundCR && foundLF))
        {
            b = inputStream.read();
            Assert.assertThat("Should not have hit EOL (yet) during chunk size read",b,not(-1));
            if (b == 0x0D)
            {
                foundCR = true;
            }
            else if (b == 0x0A)
            {
                foundLF = true;
            }
            else
            {
                foundCR = false;
                foundLF = false;
                line.append((char)b);
            }
        }
        return line.toString();
    }

    public long readChunkSize() throws IOException
    {
        StringBuilder chunkSize = new StringBuilder();
        String validHex = "0123456789ABCDEF";
        boolean foundCR = false;
        boolean foundLF = false;
        int b;
        while (!(foundCR && foundLF))
        {
            b = inputStream.read();
            Assert.assertThat("Should not have hit EOL (yet) during chunk size read",b,not(-1));
            if (b == 0x0D)
            {
                foundCR = true;
            }
            else if (b == 0x0A)
            {
                foundLF = true;
            }
            else
            {
                foundCR = false;
                foundLF = false;
                // Must be valid char
                char c = (char)b;
                if (validHex.indexOf(c) >= 0)
                {
                    chunkSize.append(c);
                }
                else
                {
                    Assert.fail(String.format("Encountered invalid chunk size byte 0x%X",b));
                }
            }
        }
        return Long.parseLong(chunkSize.toString(),16);
    }

    public int readBody(OutputStream stream, int size) throws IOException
    {
        int left = size;
        while (left > 0)
        {
            int val = inputStream.read();
            try
            {
                if (left % 1000 == 0)
                    Thread.sleep(10);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            if (val == (-1))
            {
                Assert.fail(String.format("Encountered an early EOL (expected another %,d bytes)",left));
            }
            stream.write(val);
            left--;
        }
        return size - left;
    }

    public byte[] readResponseBody(int size) throws IOException
    {
        byte partial[] = new byte[size];
        int readBytes = 0;
        int bytesLeft = size;
        while (readBytes < size)
        {
            int len = inputStream.read(partial,readBytes,bytesLeft);
            Assert.assertThat("Read should not have hit EOL yet",len,not(-1));
            //System.out.printf("Read %,d bytes%n",len);
            if (len > 0)
            {
                readBytes += len;
                bytesLeft -= len;
            }
        }
        return partial;
    }

    public OutputStream getOutputStream()
    {
        return outputStream;
    }

    public InputStream getInputStream()
    {
        return inputStream;
    }

    public SocketAddress getEndpoint()
    {
        return endpoint;
    }

    public Socket getSocket()
    {
        return socket;
    }

    public void disconnect() throws IOException
    {
        LOG.debug("disconnect");
        socket.close();
    }

    public int getContentLength(String respHeader)
    {
        Pattern pat = Pattern.compile("Content-Length: ([0-9]*)",Pattern.CASE_INSENSITIVE);
        Matcher mat = pat.matcher(respHeader);
        if (mat.find())
        {
            try
            {
                return Integer.parseInt(mat.group(1));
            }
            catch (NumberFormatException e)
            {
                return -1;
            }
        }
        else
        {
            // Undefined content length
            return -1;
        }
    }

}

Back to the top