1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.testing;
15
16 import java.io.IOException;
17 import java.util.Enumeration;
18
19 import javax.servlet.http.Cookie;
20
21 import org.eclipse.jetty.http.HttpFields;
22 import org.eclipse.jetty.http.HttpGenerator;
23 import org.eclipse.jetty.http.HttpHeaders;
24 import org.eclipse.jetty.http.HttpParser;
25 import org.eclipse.jetty.http.HttpVersions;
26 import org.eclipse.jetty.http.MimeTypes;
27 import org.eclipse.jetty.io.Buffer;
28 import org.eclipse.jetty.io.ByteArrayBuffer;
29 import org.eclipse.jetty.io.EofException;
30 import org.eclipse.jetty.io.SimpleBuffers;
31 import org.eclipse.jetty.io.View;
32 import org.eclipse.jetty.io.bio.StringEndPoint;
33 import org.eclipse.jetty.util.ByteArrayOutputStream2;
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 public class HttpTester
59 {
60 protected HttpFields _fields=new HttpFields();
61 protected String _method;
62 protected String _uri;
63 protected String _version;
64 protected int _status;
65 protected String _reason;
66 protected ByteArrayOutputStream2 _parsedContent;
67 protected byte[] _genContent;
68
69 private String _charset, _defaultCharset;
70 private Buffer _contentType;
71
72 public HttpTester()
73 {
74 this("UTF-8");
75 }
76
77 public HttpTester(String charset)
78 {
79 _defaultCharset = charset;
80 }
81
82 public void reset()
83 {
84 _fields.clear();
85 _method=null;
86 _uri=null;
87 _version=null;
88 _status=0;
89 _reason=null;
90 _parsedContent=null;
91 _genContent=null;
92 }
93
94 private String getString(Buffer buffer)
95 {
96 return getString(buffer.asArray());
97 }
98
99 private String getString(byte[] b)
100 {
101 if(_charset==null)
102 return new String(b);
103 try
104 {
105 return new String(b, _charset);
106 }
107 catch(Exception e)
108 {
109 return new String(b);
110 }
111 }
112
113 private byte[] getByteArray(String str)
114 {
115 if(_charset==null)
116 return str.getBytes();
117 try
118 {
119 return str.getBytes(_charset);
120 }
121 catch(Exception e)
122 {
123 return str.getBytes();
124 }
125 }
126
127
128
129
130
131
132
133
134 public String parse(String rawHTTP, boolean isHeadResponse) throws IOException
135 {
136 _charset = _defaultCharset;
137 ByteArrayBuffer buf = new ByteArrayBuffer(getByteArray(rawHTTP));
138 View view = new View(buf);
139 PH ph = new PH();
140 HttpParser parser = new HttpParser(view,ph);
141 parser.setHeadResponse(isHeadResponse);
142 parser.parse();
143 if (ph.isEarlyEOF())
144 throw new EofException();
145 return getString(view.asArray());
146 }
147
148
149
150
151
152
153
154
155
156 public String parse(String rawHTTP) throws IOException
157 {
158 return parse(rawHTTP, false);
159 }
160
161
162
163
164
165
166
167
168 public byte[] parse(byte[] rawHTTP, boolean isHeadResponse) throws IOException
169 {
170 _charset = _defaultCharset;
171 ByteArrayBuffer buf = new ByteArrayBuffer(rawHTTP);
172 View view = new View(buf);
173 PH ph = new PH();
174 HttpParser parser = new HttpParser(view,ph);
175 parser.setHeadResponse(isHeadResponse);
176 parser.parse();
177 if (ph.isEarlyEOF())
178 throw new EofException();
179 return view.asArray();
180 }
181
182
183
184
185
186
187
188
189 public byte[] parse(byte[] rawHTTP) throws IOException
190 {
191 return parse(rawHTTP, false);
192 }
193
194
195 public String generate() throws IOException
196 {
197 _charset = _defaultCharset;
198 _contentType = _fields.get(HttpHeaders.CONTENT_TYPE_BUFFER);
199 if(_contentType!=null)
200 {
201 String charset = MimeTypes.getCharsetFromContentType(_contentType);
202 if(charset!=null)
203 _charset = charset;
204 }
205 Buffer bb=new ByteArrayBuffer(32*1024 + (_genContent!=null?_genContent.length:0));
206 Buffer sb=new ByteArrayBuffer(4*1024);
207 StringEndPoint endp = new StringEndPoint(_charset);
208 HttpGenerator generator = new HttpGenerator(new SimpleBuffers(sb,bb),endp);
209
210 if (_method!=null)
211 {
212 generator.setRequest(getMethod(),getURI());
213 if (_version==null)
214 generator.setVersion(HttpVersions.HTTP_1_1_ORDINAL);
215 else
216 generator.setVersion(HttpVersions.CACHE.getOrdinal(HttpVersions.CACHE.lookup(_version)));
217 generator.completeHeader(_fields,false);
218 if (_genContent!=null)
219 generator.addContent(new View(new ByteArrayBuffer(_genContent)),false);
220 else if (_parsedContent!=null)
221 generator.addContent(new ByteArrayBuffer(_parsedContent.toByteArray()),false);
222 }
223
224 generator.complete();
225 generator.flushBuffer();
226 return endp.getOutput();
227 }
228
229
230
231
232
233 public String getMethod()
234 {
235 return _method;
236 }
237
238
239
240
241
242 public void setMethod(String method)
243 {
244 _method=method;
245 }
246
247
248
249
250
251 public String getReason()
252 {
253 return _reason;
254 }
255
256
257
258
259
260 public void setReason(String reason)
261 {
262 _reason=reason;
263 }
264
265
266
267
268
269 public int getStatus()
270 {
271 return _status;
272 }
273
274
275
276
277
278 public void setStatus(int status)
279 {
280 _status=status;
281 }
282
283
284
285
286
287 public String getURI()
288 {
289 return _uri;
290 }
291
292
293
294
295
296 public void setURI(String uri)
297 {
298 _uri=uri;
299 }
300
301
302
303
304
305 public String getVersion()
306 {
307 return _version;
308 }
309
310
311
312
313
314 public void setVersion(String version)
315 {
316 _version=version;
317 }
318
319
320 public String getContentType()
321 {
322 return getString(_contentType);
323 }
324
325
326 public String getCharacterEncoding()
327 {
328 return _charset;
329 }
330
331
332
333
334
335
336
337
338 public void addHeader(String name, String value) throws IllegalArgumentException
339 {
340 _fields.add(name,value);
341 }
342
343
344
345
346
347
348
349 public void addDateHeader(String name, long date)
350 {
351 _fields.addDateField(name,date);
352 }
353
354
355
356
357
358
359
360 public void addLongHeader(String name, long value)
361 {
362 _fields.addLongField(name,value);
363 }
364
365
366
367
368
369
370 public void addSetCookie(Cookie cookie)
371 {
372 _fields.addSetCookie(
373 cookie.getName(),
374 cookie.getValue(),
375 cookie.getDomain(),
376 cookie.getPath(),
377 cookie.getMaxAge(),
378 cookie.getComment(),
379 cookie.getSecure(),
380 false,
381 cookie.getVersion());
382 }
383
384
385
386
387
388
389
390 public long getDateHeader(String name)
391 {
392 return _fields.getDateField(name);
393 }
394
395
396
397
398
399
400 public Enumeration getHeaderNames()
401 {
402 return _fields.getFieldNames();
403 }
404
405
406
407
408
409
410
411
412 public long getLongHeader(String name) throws NumberFormatException
413 {
414 return _fields.getLongField(name);
415 }
416
417
418
419
420
421
422
423 public String getHeader(String name)
424 {
425 return _fields.getStringField(name);
426 }
427
428
429
430
431
432
433
434 public Enumeration getHeaderValues(String name)
435 {
436 return _fields.getValues(name);
437 }
438
439
440
441
442
443
444
445 public void setHeader(String name, String value)
446 {
447 if (HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(name))
448 setContentType(value);
449 else
450 _fields.put(name,value);
451
452 }
453
454
455 public void setContentType(String value)
456 {
457 _contentType = MimeTypes.CACHE.lookup(value);
458 _charset = MimeTypes.getCharsetFromContentType(_contentType);
459 _fields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType);
460 }
461
462
463
464
465
466
467
468 public void setDateHeader(String name, long date)
469 {
470 _fields.putDateField(name,date);
471 }
472
473
474
475
476
477
478
479 public void setLongHeader(String name, long value)
480 {
481 _fields.putLongField(name,value);
482 }
483
484
485
486
487
488
489 public void removeHeader(String name)
490 {
491 _fields.remove(name);
492 }
493
494
495 public String getContent()
496 {
497 if (_parsedContent!=null)
498 return getString(_parsedContent.toByteArray());
499 if (_genContent!=null)
500 return getString(_genContent);
501 return null;
502 }
503
504
505 public byte[] getContentBytes()
506 {
507 if (_parsedContent!=null)
508 return _parsedContent.toByteArray();
509 if (_genContent!=null)
510 return _genContent;
511 return null;
512 }
513
514
515 public void setContent(String content)
516 {
517 _parsedContent=null;
518 if (content!=null)
519 {
520 _genContent=getByteArray(content);
521 setLongHeader(HttpHeaders.CONTENT_LENGTH,_genContent.length);
522 }
523 else
524 {
525 removeHeader(HttpHeaders.CONTENT_LENGTH);
526 _genContent=null;
527 }
528 }
529
530
531 private class PH extends HttpParser.EventHandler
532 {
533 private volatile boolean _earlyEOF;
534
535 @Override
536 public void startRequest(Buffer method, Buffer url, Buffer version) throws IOException
537 {
538 reset();
539 _method=getString(method);
540 _uri=getString(url);
541 _version=getString(version);
542 }
543
544 @Override
545 public void startResponse(Buffer version, int status, Buffer reason) throws IOException
546 {
547 reset();
548 _version=getString(version);
549 _status=status;
550 _reason=getString(reason);
551 }
552
553 @Override
554 public void parsedHeader(Buffer name, Buffer value) throws IOException
555 {
556 _fields.add(name,value);
557 }
558
559 @Override
560 public void headerComplete() throws IOException
561 {
562 _contentType = _fields.get(HttpHeaders.CONTENT_TYPE_BUFFER);
563 if(_contentType!=null)
564 {
565 String charset = MimeTypes.getCharsetFromContentType(_contentType);
566 if(charset!=null)
567 _charset = charset;
568 }
569 }
570
571 @Override
572 public void messageComplete(long contextLength) throws IOException
573 {
574 }
575
576 @Override
577 public void content(Buffer ref) throws IOException
578 {
579 if (_parsedContent==null)
580 _parsedContent=new ByteArrayOutputStream2();
581 _parsedContent.write(ref.asArray());
582 }
583
584 @Override
585 public void earlyEOF()
586 {
587 _earlyEOF = true;
588 }
589
590 public boolean isEarlyEOF()
591 {
592 return _earlyEOF;
593 }
594
595 }
596
597 }