2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.charset.Charset;
25 import java.nio.charset.StandardCharsets;
27 import org.eclipse.jetty.http.HttpGenerator.RequestInfo;
28 import org.eclipse.jetty.http.HttpGenerator.ResponseInfo;
29 import org.eclipse.jetty.util.BufferUtil;
30 import org.eclipse.jetty.util.StringUtil;
32 public class HttpTester
38 public static Request newRequest()
43 public static Request parseRequest(String request)
45 Request r=new Request();
46 HttpParser parser =new HttpParser(r);
47 parser.parseNext(BufferUtil.toBuffer(request));
51 public static Request parseRequest(ByteBuffer request)
53 Request r=new Request();
54 HttpParser parser =new HttpParser(r);
55 parser.parseNext(request);
59 public static Response parseResponse(String response)
61 Response r=new Response();
62 HttpParser parser =new HttpParser(r);
63 parser.parseNext(BufferUtil.toBuffer(response));
67 public static Response parseResponse(ByteBuffer response)
69 Response r=new Response();
70 HttpParser parser =new HttpParser(r);
71 parser.parseNext(response);
76 public abstract static class Message extends HttpFields implements HttpParser.HttpHandler<ByteBuffer>
78 ByteArrayOutputStream _content;
79 HttpVersion _version=HttpVersion.HTTP_1_0;
81 public HttpVersion getVersion()
86 public void setVersion(String version)
88 setVersion(HttpVersion.CACHE.get(version));
91 public void setVersion(HttpVersion version)
96 public void setContent(byte[] bytes)
100 _content=new ByteArrayOutputStream();
101 _content.write(bytes);
103 catch (IOException e)
105 throw new RuntimeException(e);
109 public void setContent(String content)
113 _content=new ByteArrayOutputStream();
114 _content.write(StringUtil.getBytes(content));
116 catch (IOException e)
118 throw new RuntimeException(e);
122 public void setContent(ByteBuffer content)
126 _content=new ByteArrayOutputStream();
127 _content.write(BufferUtil.toArray(content));
129 catch (IOException e)
131 throw new RuntimeException(e);
135 public boolean parsedHeader(HttpField field)
137 put(field.getName(),field.getValue());
142 public boolean messageComplete()
148 public boolean headerComplete()
150 _content=new ByteArrayOutputStream();
155 public void earlyEOF()
160 public boolean content(ByteBuffer ref)
164 _content.write(BufferUtil.toArray(ref));
166 catch (IOException e)
168 throw new RuntimeException(e);
174 public void badMessage(int status, String reason)
176 throw new RuntimeException(reason);
179 public ByteBuffer generate()
183 HttpGenerator generator = new HttpGenerator();
184 HttpGenerator.Info info = getInfo();
185 // System.err.println(info.getClass());
186 // System.err.println(info);
188 ByteArrayOutputStream out = new ByteArrayOutputStream();
189 ByteBuffer header=null;
190 ByteBuffer chunk=null;
191 ByteBuffer content=_content==null?null:ByteBuffer.wrap(_content.toByteArray());
194 loop: while(!generator.isEnd())
196 HttpGenerator.Result result = info instanceof RequestInfo
197 ?generator.generateRequest((RequestInfo)info,header,chunk,content,true)
198 :generator.generateResponse((ResponseInfo)info,header,chunk,content,true);
202 header=BufferUtil.allocate(8192);
206 chunk=BufferUtil.allocate(HttpGenerator.CHUNK_SIZE);
210 throw new IllegalStateException();
213 if (BufferUtil.hasContent(header))
215 out.write(BufferUtil.toArray(header));
216 BufferUtil.clear(header);
218 if (BufferUtil.hasContent(chunk))
220 out.write(BufferUtil.toArray(chunk));
221 BufferUtil.clear(chunk);
223 if (BufferUtil.hasContent(content))
225 out.write(BufferUtil.toArray(content));
226 BufferUtil.clear(content);
235 return ByteBuffer.wrap(out.toByteArray());
237 catch (IOException e)
239 throw new RuntimeException(e);
243 abstract public HttpGenerator.Info getInfo();
246 public int getHeaderCacheSize()
253 public static class Request extends Message implements HttpParser.RequestHandler<ByteBuffer>
255 private String _method;
259 public boolean startRequest(HttpMethod method, String methodString, ByteBuffer uri, HttpVersion version)
261 _method=methodString;
262 _uri=BufferUtil.toUTF8String(uri);
267 public String getMethod()
272 public String getUri()
277 public void setMethod(String method)
282 public void setURI(String uri)
288 public HttpGenerator.RequestInfo getInfo()
290 return new HttpGenerator.RequestInfo(_version,this,_content==null?0:_content.size(),_method,_uri);
294 public String toString()
296 return String.format("%s %s %s\n%s\n",_method,_uri,_version,super.toString());
299 public void setHeader(String name, String value)
305 public boolean parsedHostHeader(String host,int port)
311 public static class Response extends Message implements HttpParser.ResponseHandler<ByteBuffer>
314 private String _reason;
317 public boolean startResponse(HttpVersion version, int status, String reason)
325 public int getStatus()
330 public String getReason()
335 public byte[] getContentBytes()
339 return _content.toByteArray();
342 public String getContent()
346 byte[] bytes=_content.toByteArray();
348 String content_type=get(HttpHeader.CONTENT_TYPE);
349 String encoding=MimeTypes.getCharsetFromContentType(content_type);
350 Charset charset=encoding==null?StandardCharsets.UTF_8:Charset.forName(encoding);
352 return new String(bytes,charset);
356 public HttpGenerator.ResponseInfo getInfo()
358 return new HttpGenerator.ResponseInfo(_version,this,_content==null?-1:_content.size(),_status,_reason,false);
362 public String toString()
364 return String.format("%s %s %s\n%s\n",_version,_status,_reason,super.toString());