comparison src/org/eclipse/jetty/http/HttpGenerator.java @ 1058:419bf9c03d84

remove HttpGenerator._method
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2016 04:17:22 -0700
parents afc9610dc12e
children 013939bfc9e8
comparison
equal deleted inserted replaced
1057:afc9610dc12e 1058:419bf9c03d84
271 LOG.debug("",e); 271 LOG.debug("",e);
272 throw new InterruptedIOException(e.toString()); 272 throw new InterruptedIOException(e.toString());
273 } 273 }
274 } 274 }
275 275
276 boolean isRequest()
277 {
278 return _method!=null;
279 }
280
281 private boolean isResponse()
282 {
283 return _method==null;
284 }
285
286 public void completeHeader(HttpFields fields, boolean allContentAdded) throws IOException 276 public void completeHeader(HttpFields fields, boolean allContentAdded) throws IOException
287 { 277 {
288 if (_state != STATE_HEADER) 278 if (_state != STATE_HEADER)
289 return; 279 return;
290 280
291 // handle a reset 281 // handle a reset
292 if (isResponse() && _status==0) 282 if (_status==0)
293 throw new EofException(); 283 throw new EofException();
294 284
295 if (_last && !allContentAdded) 285 if (_last && !allContentAdded)
296 throw new IllegalStateException("last?"); 286 throw new IllegalStateException("last?");
297 _last = _last | allContentAdded; 287 _last = _last | allContentAdded;
302 292
303 boolean has_server = false; 293 boolean has_server = false;
304 294
305 try 295 try
306 { 296 {
307 if (isRequest()) 297 // Responses
308 { 298 if (_version == HttpVersions.HTTP_0_9_ORDINAL)
309 _persistent = true; 299 {
310 300 _persistent = false;
311 if (_version == HttpVersions.HTTP_0_9_ORDINAL) 301 _contentLength = HttpTokens.EOF_CONTENT;
312 { 302 _state = STATE_CONTENT;
313 _contentLength = HttpTokens.NO_CONTENT; 303 return;
314 _header.put(_method);
315 _header.put((byte)' ');
316 _header.put(_uri.getBytes("UTF-8")); // TODO check
317 _header.put(HttpTokens.CRLF);
318 _state = STATE_FLUSHING;
319 _noContent=true;
320 return;
321 }
322 else
323 {
324 _header.put(_method);
325 _header.put((byte)' ');
326 _header.put(_uri.getBytes("UTF-8")); // TODO check
327 _header.put((byte)' ');
328 _header.put(_version==HttpVersions.HTTP_1_0_ORDINAL?HttpVersions.HTTP_1_0_BYTES:HttpVersions.HTTP_1_1_BYTES);
329 _header.put(HttpTokens.CRLF);
330 }
331 } 304 }
332 else 305 else
333 { 306 {
334 // Responses 307 if (_persistent==null)
335 if (_version == HttpVersions.HTTP_0_9_ORDINAL) 308 _persistent = (_version > HttpVersions.HTTP_1_0_ORDINAL);
336 { 309
337 _persistent = false; 310 // add response line
338 _contentLength = HttpTokens.EOF_CONTENT; 311 Status status = _status<__status.length?__status[_status]:null;
339 _state = STATE_CONTENT; 312
340 return; 313 if (status==null)
341 } 314 {
342 else 315 _header.put(HttpVersions.HTTP_1_1_BYTES);
343 { 316 _header.put((byte) ' ');
344 if (_persistent==null) 317 _header.put((byte) ('0' + _status / 100));
345 _persistent = (_version > HttpVersions.HTTP_1_0_ORDINAL); 318 _header.put((byte) ('0' + (_status % 100) / 10));
346 319 _header.put((byte) ('0' + (_status % 10)));
347 // add response line 320 _header.put((byte) ' ');
348 Status status = _status<__status.length?__status[_status]:null; 321 if (_reason==null)
349
350 if (status==null)
351 { 322 {
352 _header.put(HttpVersions.HTTP_1_1_BYTES);
353 _header.put((byte) ' ');
354 _header.put((byte) ('0' + _status / 100)); 323 _header.put((byte) ('0' + _status / 100));
355 _header.put((byte) ('0' + (_status % 100) / 10)); 324 _header.put((byte) ('0' + (_status % 100) / 10));
356 _header.put((byte) ('0' + (_status % 10))); 325 _header.put((byte) ('0' + (_status % 10)));
357 _header.put((byte) ' '); 326 }
358 if (_reason==null) 327 else
359 { 328 _header.put(_reason);
360 _header.put((byte) ('0' + _status / 100)); 329 _header.put(HttpTokens.CRLF);
361 _header.put((byte) ('0' + (_status % 100) / 10)); 330 }
362 _header.put((byte) ('0' + (_status % 10))); 331 else
363 } 332 {
364 else 333 if (_reason==null)
365 _header.put(_reason); 334 _header.put(status._responseLine);
335 else
336 {
337 _header.put(status._schemeCode);
338 _header.put(_reason);
366 _header.put(HttpTokens.CRLF); 339 _header.put(HttpTokens.CRLF);
367 } 340 }
368 else 341 }
342
343 if (_status<200 && _status>=100 )
344 {
345 _noContent=true;
346 _content=null;
347 if (_buffer!=null)
348 _buffer.clear();
349 // end the header.
350
351 if (_status!=101 )
369 { 352 {
370 if (_reason==null) 353 _header.put(HttpTokens.CRLF);
371 _header.put(status._responseLine); 354 _state = STATE_CONTENT;
372 else 355 return;
373 {
374 _header.put(status._schemeCode);
375 _header.put(_reason);
376 _header.put(HttpTokens.CRLF);
377 }
378 } 356 }
379 357 }
380 if (_status<200 && _status>=100 ) 358 else if (_status==204 || _status==304)
381 { 359 {
382 _noContent=true; 360 _noContent=true;
383 _content=null; 361 _content=null;
384 if (_buffer!=null) 362 if (_buffer!=null)
385 _buffer.clear(); 363 _buffer.clear();
386 // end the header.
387
388 if (_status!=101 )
389 {
390 _header.put(HttpTokens.CRLF);
391 _state = STATE_CONTENT;
392 return;
393 }
394 }
395 else if (_status==204 || _status==304)
396 {
397 _noContent=true;
398 _content=null;
399 if (_buffer!=null)
400 _buffer.clear();
401 }
402 } 364 }
403 } 365 }
404 366
405 // key field values 367 // key field values
406 HttpFields.Field content_length = null; 368 HttpFields.Field content_length = null;
447 transfer_encoding = field; 409 transfer_encoding = field;
448 // Do NOT add yet! 410 // Do NOT add yet!
449 break; 411 break;
450 412
451 case HttpHeaders.CONNECTION_ORDINAL: 413 case HttpHeaders.CONNECTION_ORDINAL:
452 if (isRequest())
453 field.putTo(_header);
454
455 int connection_value = field.getValueOrdinal(); 414 int connection_value = field.getValueOrdinal();
456 switch (connection_value) 415 switch (connection_value)
457 { 416 {
458 case -1: 417 case -1:
459 { 418 {
466 { 425 {
467 switch(ord) 426 switch(ord)
468 { 427 {
469 case HttpHeaderValues.CLOSE_ORDINAL: 428 case HttpHeaderValues.CLOSE_ORDINAL:
470 close=true; 429 close=true;
471 if (isResponse()) 430 _persistent = false;
472 _persistent = false;
473 keep_alive=false; 431 keep_alive=false;
474 if (!_persistent && isResponse() && _contentLength == HttpTokens.UNKNOWN_CONTENT) 432 if (!_persistent && _contentLength == HttpTokens.UNKNOWN_CONTENT)
475 _contentLength = HttpTokens.EOF_CONTENT; 433 _contentLength = HttpTokens.EOF_CONTENT;
476 break; 434 break;
477 435
478 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: 436 case HttpHeaderValues.KEEP_ALIVE_ORDINAL:
479 if (_version == HttpVersions.HTTP_1_0_ORDINAL) 437 if (_version == HttpVersions.HTTP_1_0_ORDINAL)
480 { 438 {
481 keep_alive = true; 439 keep_alive = true;
482 if (isResponse()) 440 _persistent = true;
483 _persistent = true;
484 } 441 }
485 break; 442 break;
486 443
487 default: 444 default:
488 if (connection==null) 445 if (connection==null)
505 break; 462 break;
506 } 463 }
507 case HttpHeaderValues.UPGRADE_ORDINAL: 464 case HttpHeaderValues.UPGRADE_ORDINAL:
508 { 465 {
509 // special case for websocket connection ordering 466 // special case for websocket connection ordering
510 if (isResponse()) 467 field.putTo(_header);
511 { 468 continue;
512 field.putTo(_header);
513 continue;
514 }
515 } 469 }
516 case HttpHeaderValues.CLOSE_ORDINAL: 470 case HttpHeaderValues.CLOSE_ORDINAL:
517 { 471 {
518 close=true; 472 close=true;
519 if (isResponse()) 473 _persistent=false;
520 _persistent=false; 474 if (!_persistent && _contentLength == HttpTokens.UNKNOWN_CONTENT)
521 if (!_persistent && isResponse() && _contentLength == HttpTokens.UNKNOWN_CONTENT)
522 _contentLength = HttpTokens.EOF_CONTENT; 475 _contentLength = HttpTokens.EOF_CONTENT;
523 break; 476 break;
524 } 477 }
525 case HttpHeaderValues.KEEP_ALIVE_ORDINAL: 478 case HttpHeaderValues.KEEP_ALIVE_ORDINAL:
526 { 479 {
527 if (_version == HttpVersions.HTTP_1_0_ORDINAL) 480 if (_version == HttpVersions.HTTP_1_0_ORDINAL)
528 { 481 {
529 keep_alive = true; 482 keep_alive = true;
530 if (isResponse()) 483 _persistent=true;
531 _persistent=true;
532 } 484 }
533 break; 485 break;
534 } 486 }
535 default: 487 default:
536 { 488 {
571 case HttpTokens.UNKNOWN_CONTENT: 523 case HttpTokens.UNKNOWN_CONTENT:
572 // It may be that we have no _content, or perhaps _content just has not been 524 // It may be that we have no _content, or perhaps _content just has not been
573 // written yet? 525 // written yet?
574 526
575 // Response known not to have a body 527 // Response known not to have a body
576 if (_contentWritten == 0 && isResponse() && (_status < 200 || _status == 204 || _status == 304)) 528 if (_contentWritten == 0 && (_status < 200 || _status == 204 || _status == 304))
577 _contentLength = HttpTokens.NO_CONTENT; 529 _contentLength = HttpTokens.NO_CONTENT;
578 else if (_last) 530 else if (_last)
579 { 531 {
580 // we have seen all the _content there is 532 // we have seen all the _content there is
581 _contentLength = _contentWritten; 533 _contentLength = _contentWritten;
582 if (content_length == null && (isResponse() || _contentLength>0 || content_type ) && !_noContent) 534 if (content_length == null && !_noContent)
583 { 535 {
584 // known length but not actually set. 536 // known length but not actually set.
585 _header.put(HttpHeaders.CONTENT_LENGTH_BYTES); 537 _header.put(HttpHeaders.CONTENT_LENGTH_BYTES);
586 _header.put(HttpTokens.COLON); 538 _header.put(HttpTokens.COLON);
587 _header.put((byte) ' '); 539 _header.put((byte) ' ');
591 } 543 }
592 else 544 else
593 { 545 {
594 // No idea, so we must assume that a body is coming 546 // No idea, so we must assume that a body is coming
595 _contentLength = (!_persistent || _version < HttpVersions.HTTP_1_1_ORDINAL ) ? HttpTokens.EOF_CONTENT : HttpTokens.CHUNKED_CONTENT; 547 _contentLength = (!_persistent || _version < HttpVersions.HTTP_1_1_ORDINAL ) ? HttpTokens.EOF_CONTENT : HttpTokens.CHUNKED_CONTENT;
596 if (isRequest() && _contentLength==HttpTokens.EOF_CONTENT)
597 {
598 _contentLength=HttpTokens.NO_CONTENT;
599 _noContent=true;
600 }
601 } 548 }
602 break; 549 break;
603 550
604 case HttpTokens.NO_CONTENT: 551 case HttpTokens.NO_CONTENT:
605 if (content_length == null && isResponse() && _status >= 200 && _status != 204 && _status != 304) 552 if (content_length == null && _status >= 200 && _status != 204 && _status != 304)
606 _header.put(CONTENT_LENGTH_0); 553 _header.put(CONTENT_LENGTH_0);
607 break; 554 break;
608 555
609 case HttpTokens.EOF_CONTENT: 556 case HttpTokens.EOF_CONTENT:
610 _persistent = isRequest(); 557 _persistent = false;
611 break; 558 break;
612 559
613 case HttpTokens.CHUNKED_CONTENT: 560 case HttpTokens.CHUNKED_CONTENT:
614 break; 561 break;
615 562
639 { 586 {
640 keep_alive=false; 587 keep_alive=false;
641 _persistent=false; 588 _persistent=false;
642 } 589 }
643 590
644 if (isResponse()) 591 if (!_persistent && (close || _version > HttpVersions.HTTP_1_0_ORDINAL))
645 { 592 {
646 if (!_persistent && (close || _version > HttpVersions.HTTP_1_0_ORDINAL)) 593 _header.put(CONNECTION_CLOSE);
647 { 594 if (connection!=null)
648 _header.put(CONNECTION_CLOSE); 595 {
649 if (connection!=null) 596 _header.setPutIndex(_header.putIndex()-2);
650 { 597 _header.put((byte)',');
651 _header.setPutIndex(_header.putIndex()-2);
652 _header.put((byte)',');
653 _header.put(connection.toString().getBytes());
654 _header.put(CRLF);
655 }
656 }
657 else if (keep_alive)
658 {
659 _header.put(CONNECTION_KEEP_ALIVE);
660 if (connection!=null)
661 {
662 _header.setPutIndex(_header.putIndex()-2);
663 _header.put((byte)',');
664 _header.put(connection.toString().getBytes());
665 _header.put(CRLF);
666 }
667 }
668 else if (connection!=null)
669 {
670 _header.put(CONNECTION_);
671 _header.put(connection.toString().getBytes()); 598 _header.put(connection.toString().getBytes());
672 _header.put(CRLF); 599 _header.put(CRLF);
673 } 600 }
601 }
602 else if (keep_alive)
603 {
604 _header.put(CONNECTION_KEEP_ALIVE);
605 if (connection!=null)
606 {
607 _header.setPutIndex(_header.putIndex()-2);
608 _header.put((byte)',');
609 _header.put(connection.toString().getBytes());
610 _header.put(CRLF);
611 }
612 }
613 else if (connection!=null)
614 {
615 _header.put(CONNECTION_);
616 _header.put(connection.toString().getBytes());
617 _header.put(CRLF);
674 } 618 }
675 619
676 if (!has_server && _status>199) 620 if (!has_server && _status>199)
677 _header.put(SERVER); 621 _header.put(SERVER);
678 622
796 if (!_needCRLF && !_needEOC && (_content==null || _content.remaining()==0)) 740 if (!_needCRLF && !_needEOC && (_content==null || _content.remaining()==0))
797 { 741 {
798 if (_state == STATE_FLUSHING) 742 if (_state == STATE_FLUSHING)
799 _state = STATE_END; 743 _state = STATE_END;
800 744
801 if (_state==STATE_END && _persistent != null && !_persistent && _status!=100 && _method==null) 745 if (_state==STATE_END && _persistent != null && !_persistent && _status!=100)
802 _endp.shutdownOutput(); 746 _endp.shutdownOutput();
803 } 747 }
804 else 748 else
805 // Try to prepare more to write. 749 // Try to prepare more to write.
806 prepareBuffers(); 750 prepareBuffers();
1014 private int _state = STATE_HEADER; 958 private int _state = STATE_HEADER;
1015 959
1016 private int _status = 0; 960 private int _status = 0;
1017 private int _version = HttpVersions.HTTP_1_1_ORDINAL; 961 private int _version = HttpVersions.HTTP_1_1_ORDINAL;
1018 private JBuffer _reason; 962 private JBuffer _reason;
1019 private JBuffer _method;
1020 private String _uri; 963 private String _uri;
1021 964
1022 private long _contentWritten = 0; 965 private long _contentWritten = 0;
1023 private long _contentLength = HttpTokens.UNKNOWN_CONTENT; 966 private long _contentLength = HttpTokens.UNKNOWN_CONTENT;
1024 private boolean _last = false; 967 private boolean _last = false;
1077 return _state == STATE_END; 1020 return _state == STATE_END;
1078 } 1021 }
1079 1022
1080 public final boolean isIdle() 1023 public final boolean isIdle()
1081 { 1024 {
1082 return _state == STATE_HEADER && _method==null && _status==0; 1025 return _state == STATE_HEADER && _status==0;
1083 } 1026 }
1084 1027
1085 public final boolean isCommitted() 1028 public final boolean isCommitted()
1086 { 1029 {
1087 return _state != STATE_HEADER; 1030 return _state != STATE_HEADER;
1107 */ 1050 */
1108 public final boolean isPersistent() 1051 public final boolean isPersistent()
1109 { 1052 {
1110 return _persistent!=null 1053 return _persistent!=null
1111 ?_persistent.booleanValue() 1054 ?_persistent.booleanValue()
1112 :(isRequest()?true:_version>HttpVersions.HTTP_1_0_ORDINAL); 1055 :_version>HttpVersions.HTTP_1_0_ORDINAL;
1113 } 1056 }
1114 1057
1115 public final void setPersistent(boolean persistent) 1058 public final void setPersistent(boolean persistent)
1116 { 1059 {
1117 _persistent = persistent; 1060 _persistent = persistent;
1125 public final void setVersion(int version) 1068 public final void setVersion(int version)
1126 { 1069 {
1127 if (_state != STATE_HEADER) 1070 if (_state != STATE_HEADER)
1128 throw new IllegalStateException("STATE!=START "+_state); 1071 throw new IllegalStateException("STATE!=START "+_state);
1129 _version = version; 1072 _version = version;
1130 if (_version==HttpVersions.HTTP_0_9_ORDINAL && _method!=null)
1131 _noContent=true;
1132 } 1073 }
1133 1074
1134 /* ------------------------------------------------------------ */ 1075 /* ------------------------------------------------------------ */
1135 /** 1076 /**
1136 * @param status The status code to send. 1077 * @param status The status code to send.
1137 * @param reason the status message to send. 1078 * @param reason the status message to send.
1138 */ 1079 */
1139 public final void setResponse(int status, String reason) 1080 public final void setResponse(int status, String reason)
1140 { 1081 {
1141 if (_state != STATE_HEADER) throw new IllegalStateException("STATE!=START"); 1082 if (_state != STATE_HEADER) throw new IllegalStateException("STATE!=START");
1142 _method=null;
1143 _status = status; 1083 _status = status;
1144 if (reason!=null) 1084 if (reason!=null)
1145 { 1085 {
1146 int len=reason.length(); 1086 int len=reason.length();
1147 1087