jetty-http-8.1.0.v20120127

16:37:31.112 INFO  jd.cli.Main - Decompiling jetty-http-8.1.0.v20120127.jar
package org.eclipse.jetty.http;

import java.io.IOException;
import java.io.InterruptedIOException;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.BufferCache.CachedBuffer;
import org.eclipse.jetty.io.BufferUtil;
import org.eclipse.jetty.io.Buffers;
import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class HttpGenerator
  extends AbstractGenerator
{
  private static final Logger LOG = Log.getLogger(HttpGenerator.class);
  private static final Status[] __status = new Status['?'];
  
  static
  {
    int versionLength = HttpVersions.HTTP_1_1_BUFFER.length();
    for (int i = 0; i < __status.length; i++)
    {
      HttpStatus.Code code = HttpStatus.getCode(i);
      if (code != null)
      {
        String reason = code.getMessage();
        byte[] bytes = new byte[versionLength + 5 + reason.length() + 2];
        HttpVersions.HTTP_1_1_BUFFER.peek(0, bytes, 0, versionLength);
        bytes[(versionLength + 0)] = 32;
        bytes[(versionLength + 1)] = ((byte)(48 + i / 100));
        bytes[(versionLength + 2)] = ((byte)(48 + i % 100 / 10));
        bytes[(versionLength + 3)] = ((byte)(48 + i % 10));
        bytes[(versionLength + 4)] = 32;
        for (int j = 0; j < reason.length(); j++) {
          bytes[(versionLength + 5 + j)] = ((byte)reason.charAt(j));
        }
        bytes[(versionLength + 5 + reason.length())] = 13;
        bytes[(versionLength + 6 + reason.length())] = 10;
        
        __status[i] = new Status(null);
        __status_reason = new ByteArrayBuffer(bytes, versionLength + 5, bytes.length - versionLength - 7, 0);
        __status_schemeCode = new ByteArrayBuffer(bytes, 0, versionLength + 5, 0);
        __status_responseLine = new ByteArrayBuffer(bytes, 0, bytes.length, 0);
      }
    }
  }
  
  public static Buffer getReasonBuffer(int code)
  {
    Status status = code < __status.length ? __status[code] : null;
    if (status != null) {
      return _reason;
    }
    return null;
  }
  
  private static final byte[] LAST_CHUNK = { 48, 13, 10, 13, 10 };
  private static final byte[] CONTENT_LENGTH_0 = StringUtil.getBytes("Content-Length: 0\r\n");
  private static final byte[] CONNECTION_KEEP_ALIVE = StringUtil.getBytes("Connection: keep-alive\r\n");
  private static final byte[] CONNECTION_CLOSE = StringUtil.getBytes("Connection: close\r\n");
  private static final byte[] CONNECTION_ = StringUtil.getBytes("Connection: ");
  private static final byte[] CRLF = StringUtil.getBytes("\r\n");
  private static final byte[] TRANSFER_ENCODING_CHUNKED = StringUtil.getBytes("Transfer-Encoding: chunked\r\n");
  private static byte[] SERVER = StringUtil.getBytes("Server: Jetty(7.0.x)\r\n");
  private static final int CHUNK_SPACE = 12;
  
  public static void setServerVersion(String version)
  {
    SERVER = StringUtil.getBytes("Server: Jetty(" + version + ")\r\n");
  }
  
  private boolean _bypass = false;
  private boolean _needCRLF = false;
  private boolean _needEOC = false;
  private boolean _bufferChunked = false;
  
  public HttpGenerator(Buffers buffers, EndPoint io)
  {
    super(buffers, io);
  }
  
  public void reset()
  {
    if ((_persistent != null) && (!_persistent.booleanValue()) && (_endp != null) && (!_endp.isOutputShutdown())) {
      try
      {
        _endp.shutdownOutput();
      }
      catch (IOException e)
      {
        LOG.ignore(e);
      }
    }
    super.reset();
    if (_buffer != null) {
      _buffer.clear();
    }
    if (_header != null) {
      _header.clear();
    }
    if (_content != null) {
      _content = null;
    }
    _bypass = false;
    _needCRLF = false;
    _needEOC = false;
    _bufferChunked = false;
    _method = null;
    _uri = null;
    _noContent = false;
  }
  
  public void addContent(Buffer content, boolean last)
    throws IOException
  {
    if (_noContent) {
      throw new IllegalStateException("NO CONTENT");
    }
    if ((_last) || (_state == 4))
    {
      LOG.warn("Ignoring extra content {}", new Object[] { content });
      content.clear();
      return;
    }
    _last = last;
    if (((_content != null) && (_content.length() > 0)) || (_bufferChunked))
    {
      if (_endp.isOutputShutdown()) {
        throw new EofException();
      }
      flushBuffer();
      if ((_content != null) && (_content.length() > 0))
      {
        Buffer nc = _buffers.getBuffer(_content.length() + content.length());
        nc.put(_content);
        nc.put(content);
        content = nc;
      }
    }
    _content = content;
    _contentWritten += content.length();
    if (_head)
    {
      content.clear();
      _content = null;
    }
    else if ((_endp != null) && ((_buffer == null) || (_buffer.length() == 0)) && (_content.length() > 0) && ((_last) || ((isCommitted()) && (_content.length() > 1024))))
    {
      _bypass = true;
    }
    else if (!_bufferChunked)
    {
      if (_buffer == null) {
        _buffer = _buffers.getBuffer();
      }
      int len = _buffer.put(_content);
      _content.skip(len);
      if (_content.length() == 0) {
        _content = null;
      }
    }
  }
  
  public void sendResponse(Buffer response)
    throws IOException
  {
    if ((_noContent) || (_state != 0) || ((_content != null) && (_content.length() > 0)) || (_bufferChunked) || (_head)) {
      throw new IllegalStateException();
    }
    _last = true;
    
    _content = response;
    _bypass = true;
    _state = 3;
    
    _contentLength = (_contentWritten = response.length());
  }
  
  public boolean addContent(byte b)
    throws IOException
  {
    if (_noContent) {
      throw new IllegalStateException("NO CONTENT");
    }
    if ((_last) || (_state == 4))
    {
      LOG.warn("Ignoring extra content {}", new Object[] { Byte.valueOf(b) });
      return false;
    }
    if (((_content != null) && (_content.length() > 0)) || (_bufferChunked))
    {
      flushBuffer();
      if (((_content != null) && (_content.length() > 0)) || (_bufferChunked)) {
        throw new IllegalStateException("FULL");
      }
    }
    _contentWritten += 1L;
    if (_head) {
      return false;
    }
    if (_buffer == null) {
      _buffer = _buffers.getBuffer();
    }
    _buffer.put(b);
    
    return _buffer.space() <= (_contentLength == -2L ? 12 : 0);
  }
  
  public int prepareUncheckedAddContent()
    throws IOException
  {
    if (_noContent) {
      return -1;
    }
    if ((_last) || (_state == 4)) {
      return -1;
    }
    Buffer content = _content;
    if (((content != null) && (content.length() > 0)) || (_bufferChunked))
    {
      flushBuffer();
      if (((content != null) && (content.length() > 0)) || (_bufferChunked)) {
        throw new IllegalStateException("FULL");
      }
    }
    if (_buffer == null) {
      _buffer = _buffers.getBuffer();
    }
    _contentWritten -= _buffer.length();
    if (_head) {
      return Integer.MAX_VALUE;
    }
    return _buffer.space() - (_contentLength == -2L ? 12 : 0);
  }
  
  public boolean isBufferFull()
  {
    return (super.isBufferFull()) || (_bufferChunked) || (_bypass) || ((_contentLength == -2L) && (_buffer != null) && (_buffer.space() < 12));
  }
  
  public void send1xx(int code)
    throws IOException
  {
    if (_state != 0) {
      return;
    }
    if ((code < 100) || (code > 199)) {
      throw new IllegalArgumentException("!1xx");
    }
    Status status = __status[code];
    if (status == null) {
      throw new IllegalArgumentException(code + "?");
    }
    if (_header == null) {
      _header = _buffers.getHeader();
    }
    _header.put(_responseLine);
    _header.put(HttpTokens.CRLF);
    try
    {
      while (_header.length() > 0)
      {
        int len = _endp.flush(_header);
        if (len < 0) {
          throw new EofException();
        }
        if (len == 0) {
          Thread.sleep(100L);
        }
      }
    }
    catch (InterruptedException e)
    {
      LOG.debug(e);
      throw new InterruptedIOException(e.toString());
    }
  }
  
  public boolean isRequest()
  {
    return _method != null;
  }
  
  public boolean isResponse()
  {
    return _method == null;
  }
  
  public void completeHeader(HttpFields fields, boolean allContentAdded)
    throws IOException
  {
    if (_state != 0) {
      return;
    }
    if ((isResponse()) && (_status == 0)) {
      throw new EofException();
    }
    if ((_last) && (!allContentAdded)) {
      throw new IllegalStateException("last?");
    }
    _last |= allContentAdded;
    if (_header == null) {
      _header = _buffers.getHeader();
    }
    boolean has_server = false;
    try
    {
      if (isRequest())
      {
        _persistent = Boolean.valueOf(true);
        if (_version == 9)
        {
          _contentLength = 0L;
          _header.put(_method);
          _header.put((byte)32);
          _header.put(_uri.getBytes("UTF-8"));
          _header.put(HttpTokens.CRLF);
          _state = 3;
          _noContent = true;
          return;
        }
        _header.put(_method);
        _header.put((byte)32);
        _header.put(_uri.getBytes("UTF-8"));
        _header.put((byte)32);
        _header.put(_version == 10 ? HttpVersions.HTTP_1_0_BUFFER : HttpVersions.HTTP_1_1_BUFFER);
        _header.put(HttpTokens.CRLF);
      }
      else
      {
        if (_version == 9)
        {
          _persistent = Boolean.valueOf(false);
          _contentLength = -1L;
          _state = 2;
          return;
        }
        if (_persistent == null) {
          _persistent = Boolean.valueOf(_version > 10);
        }
        Status status = _status < __status.length ? __status[_status] : null;
        if (status == null)
        {
          _header.put(HttpVersions.HTTP_1_1_BUFFER);
          _header.put((byte)32);
          _header.put((byte)(48 + _status / 100));
          _header.put((byte)(48 + _status % 100 / 10));
          _header.put((byte)(48 + _status % 10));
          _header.put((byte)32);
          if (_reason == null)
          {
            _header.put((byte)(48 + _status / 100));
            _header.put((byte)(48 + _status % 100 / 10));
            _header.put((byte)(48 + _status % 10));
          }
          else
          {
            _header.put(_reason);
          }
          _header.put(HttpTokens.CRLF);
        }
        else if (_reason == null)
        {
          _header.put(_responseLine);
        }
        else
        {
          _header.put(_schemeCode);
          _header.put(_reason);
          _header.put(HttpTokens.CRLF);
        }
        if ((_status < 200) && (_status >= 100))
        {
          _noContent = true;
          _content = null;
          if (_buffer != null) {
            _buffer.clear();
          }
          if (_status != 101)
          {
            _header.put(HttpTokens.CRLF);
            _state = 2;
          }
        }
        else if ((_status == 204) || (_status == 304))
        {
          _noContent = true;
          _content = null;
          if (_buffer != null) {
            _buffer.clear();
          }
        }
      }
      if ((_status >= 200) && (_date != null))
      {
        _header.put(HttpHeaders.DATE_BUFFER);
        _header.put((byte)58);
        _header.put((byte)32);
        _header.put(_date);
        _header.put(CRLF);
      }
      HttpFields.Field content_length = null;
      HttpFields.Field transfer_encoding = null;
      boolean keep_alive = false;
      boolean close = false;
      boolean content_type = false;
      StringBuilder connection = null;
      if (fields != null)
      {
        int s = fields.size();
        for (int f = 0; f < s; f++)
        {
          HttpFields.Field field = fields.getField(f);
          if (field != null) {
            switch (field.getNameOrdinal())
            {
            case 12: 
              content_length = field;
              _contentLength = field.getLongValue();
              if ((_contentLength < _contentWritten) || ((_last) && (_contentLength != _contentWritten))) {
                content_length = null;
              }
              field.putTo(_header);
              break;
            case 16: 
              if (BufferUtil.isPrefix(MimeTypes.MULTIPART_BYTERANGES_BUFFER, field.getValueBuffer())) {
                _contentLength = -4L;
              }
              content_type = true;
              field.putTo(_header);
              break;
            case 5: 
              if (_version == 11) {
                transfer_encoding = field;
              }
              break;
            case 1: 
              if (isRequest()) {
                field.putTo(_header);
              }
              int connection_value = field.getValueOrdinal();
              switch (connection_value)
              {
              case -1: 
                String[] values = field.getValue().split(",");
                for (int i = 0; (values != null) && (i < values.length); i++)
                {
                  BufferCache.CachedBuffer cb = HttpHeaderValues.CACHE.get(values[i].trim());
                  if (cb != null)
                  {
                    switch (cb.getOrdinal())
                    {
                    case 1: 
                      close = true;
                      if (isResponse()) {
                        _persistent = Boolean.valueOf(false);
                      }
                      keep_alive = false;
                      if ((_persistent.booleanValue()) || (!isResponse()) || (_contentLength != -3L)) {
                        continue;
                      }
                      _contentLength = -1L; break;
                    case 5: 
                      if (_version != 10) {
                        continue;
                      }
                      keep_alive = true;
                      if (!isResponse()) {
                        continue;
                      }
                      _persistent = Boolean.valueOf(true); break;
                    default: 
                      if (connection == null) {
                        connection = new StringBuilder();
                      } else {
                        connection.append(',');
                      }
                      connection.append(values[i]);break;
                    }
                  }
                  else
                  {
                    if (connection == null) {
                      connection = new StringBuilder();
                    } else {
                      connection.append(',');
                    }
                    connection.append(values[i]);
                  }
                }
                break;
              case 11: 
                if (isResponse()) {
                  field.putTo(_header);
                }
                break;
              case 1: 
                close = true;
                if (isResponse()) {
                  _persistent = Boolean.valueOf(false);
                }
                if ((_persistent.booleanValue()) || (!isResponse()) || (_contentLength != -3L)) {
                  continue;
                }
                _contentLength = -1L; break;
              case 5: 
                if (_version != 10) {
                  continue;
                }
                keep_alive = true;
                if (!isResponse()) {
                  continue;
                }
                _persistent = Boolean.valueOf(true); break;
              }
              if (connection == null) {
                connection = new StringBuilder();
              } else {
                connection.append(',');
              }
              connection.append(field.getValue());
              
              break;
            case 48: 
              if (getSendServerVersion())
              {
                has_server = true;
                field.putTo(_header);
              }
              break;
            default: 
              field.putTo(_header);
            }
          }
        }
      }
      switch ((int)_contentLength)
      {
      case -3: 
        if ((_contentWritten == 0L) && (isResponse()) && ((_status < 200) || (_status == 204) || (_status == 304)))
        {
          _contentLength = 0L;
        }
        else if (_last)
        {
          _contentLength = _contentWritten;
          if ((content_length == null) && ((isResponse()) || (_contentLength > 0L) || (content_type)) && (!_noContent))
          {
            _header.put(HttpHeaders.CONTENT_LENGTH_BUFFER);
            _header.put((byte)58);
            _header.put((byte)32);
            BufferUtil.putDecLong(_header, _contentLength);
            _header.put(HttpTokens.CRLF);
          }
        }
        else
        {
          _contentLength = ((!_persistent.booleanValue()) || (_version < 11) ? -1L : -2L);
          if ((isRequest()) && (_contentLength == -1L))
          {
            _contentLength = 0L;
            _noContent = true;
          }
        }
        break;
      case 0: 
        if ((content_length == null) && (isResponse()) && (_status >= 200) && (_status != 204) && (_status != 304)) {
          _header.put(CONTENT_LENGTH_0);
        }
        break;
      case -1: 
        _persistent = Boolean.valueOf(isRequest());
        break;
      case -2: 
        break;
      }
      if (_contentLength == -2L) {
        if ((transfer_encoding != null) && (2 != transfer_encoding.getValueOrdinal()))
        {
          String c = transfer_encoding.getValue();
          if (c.endsWith("chunked")) {
            transfer_encoding.putTo(_header);
          } else {
            throw new IllegalArgumentException("BAD TE");
          }
        }
        else
        {
          _header.put(TRANSFER_ENCODING_CHUNKED);
        }
      }
      if (_contentLength == -1L)
      {
        keep_alive = false;
        _persistent = Boolean.valueOf(false);
      }
      if (isResponse()) {
        if ((!_persistent.booleanValue()) && ((close) || (_version > 10)))
        {
          _header.put(CONNECTION_CLOSE);
          if (connection != null)
          {
            _header.setPutIndex(_header.putIndex() - 2);
            _header.put((byte)44);
            _header.put(connection.toString().getBytes());
            _header.put(CRLF);
          }
        }
        else if (keep_alive)
        {
          _header.put(CONNECTION_KEEP_ALIVE);
          if (connection != null)
          {
            _header.setPutIndex(_header.putIndex() - 2);
            _header.put((byte)44);
            _header.put(connection.toString().getBytes());
            _header.put(CRLF);
          }
        }
        else if (connection != null)
        {
          _header.put(CONNECTION_);
          _header.put(connection.toString().getBytes());
          _header.put(CRLF);
        }
      }
      if ((!has_server) && (_status > 199) && (getSendServerVersion())) {
        _header.put(SERVER);
      }
      _header.put(HttpTokens.CRLF);
      _state = 2;
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
      throw new RuntimeException("Header>" + _header.capacity(), e);
    }
  }
  
  public void complete()
    throws IOException
  {
    if (_state == 4) {
      return;
    }
    super.complete();
    if (_state < 3)
    {
      _state = 3;
      if (_contentLength == -2L) {
        _needEOC = true;
      }
    }
    flushBuffer();
  }
  
  public int flushBuffer()
    throws IOException
  {
    try
    {
      if (_state == 0) {
        throw new IllegalStateException("State==HEADER");
      }
      prepareBuffers();
      if (_endp == null)
      {
        if ((_needCRLF) && (_buffer != null)) {
          _buffer.put(HttpTokens.CRLF);
        }
        if ((_needEOC) && (_buffer != null) && (!_head)) {
          _buffer.put(LAST_CHUNK);
        }
        _needCRLF = false;
        _needEOC = false;
        return 0;
      }
      int total = 0;
      
      int len = -1;
      int to_flush = flushMask();
      int last_flush;
      do
      {
        last_flush = to_flush;
        switch (to_flush)
        {
        case 7: 
          throw new IllegalStateException();
        case 6: 
          len = _endp.flush(_header, _buffer, null);
          break;
        case 5: 
          len = _endp.flush(_header, _content, null);
          break;
        case 4: 
          len = _endp.flush(_header);
          break;
        case 3: 
          len = _endp.flush(_buffer, _content, null);
          break;
        case 2: 
          len = _endp.flush(_buffer);
          break;
        case 1: 
          len = _endp.flush(_content);
          break;
        case 0: 
          len = 0;
          if (_header != null) {
            _header.clear();
          }
          _bypass = false;
          _bufferChunked = false;
          if (_buffer != null)
          {
            _buffer.clear();
            if (_contentLength == -2L)
            {
              _buffer.setPutIndex(12);
              _buffer.setGetIndex(12);
              if ((_content != null) && (_content.length() < _buffer.space()) && (_state != 3))
              {
                _buffer.put(_content);
                _content.clear();
                _content = null;
              }
            }
          }
          if ((!_needCRLF) && (!_needEOC) && ((_content == null) || (_content.length() == 0)))
          {
            if (_state == 3) {
              _state = 4;
            }
            if ((_state == 4) && (_persistent != null) && (!_persistent.booleanValue()) && (_status != 100) && (_method == null)) {
              _endp.shutdownOutput();
            }
          }
          else
          {
            prepareBuffers();
          }
          break;
        }
        if (len > 0) {
          total += len;
        }
        to_flush = flushMask();
      } while ((len > 0) || ((to_flush != 0) && (last_flush == 0)));
      return total;
    }
    catch (IOException e)
    {
      LOG.ignore(e);
      throw ((e instanceof EofException) ? e : new EofException(e));
    }
  }
  
  private int flushMask()
  {
    return ((_header != null) && (_header.length() > 0) ? 4 : 0) | ((_buffer != null) && (_buffer.length() > 0) ? 2 : 0) | ((_bypass) && (_content != null) && (_content.length() > 0) ? 1 : 0);
  }
  
  private void prepareBuffers()
  {
    if (!_bufferChunked)
    {
      if ((!_bypass) && (_content != null) && (_content.length() > 0) && (_buffer != null) && (_buffer.space() > 0))
      {
        int len = _buffer.put(_content);
        _content.skip(len);
        if (_content.length() == 0) {
          _content = null;
        }
      }
      if (_contentLength == -2L)
      {
        if ((_bypass) && ((_buffer == null) || (_buffer.length() == 0)) && (_content != null))
        {
          int size = _content.length();
          _bufferChunked = true;
          if (_header == null) {
            _header = _buffers.getHeader();
          }
          if (_needCRLF)
          {
            if (_header.length() > 0) {
              throw new IllegalStateException("EOC");
            }
            _header.put(HttpTokens.CRLF);
            _needCRLF = false;
          }
          BufferUtil.putHexInt(_header, size);
          _header.put(HttpTokens.CRLF);
          
          _needCRLF = true;
        }
        else if (_buffer != null)
        {
          int size = _buffer.length();
          if (size > 0)
          {
            _bufferChunked = true;
            if (_buffer.getIndex() == 12)
            {
              _buffer.poke(_buffer.getIndex() - 2, HttpTokens.CRLF, 0, 2);
              _buffer.setGetIndex(_buffer.getIndex() - 2);
              BufferUtil.prependHexInt(_buffer, size);
              if (_needCRLF)
              {
                _buffer.poke(_buffer.getIndex() - 2, HttpTokens.CRLF, 0, 2);
                _buffer.setGetIndex(_buffer.getIndex() - 2);
                _needCRLF = false;
              }
            }
            else
            {
              if (_header == null) {
                _header = _buffers.getHeader();
              }
              if (_needCRLF)
              {
                if (_header.length() > 0) {
                  throw new IllegalStateException("EOC");
                }
                _header.put(HttpTokens.CRLF);
                _needCRLF = false;
              }
              BufferUtil.putHexInt(_header, size);
              _header.put(HttpTokens.CRLF);
            }
            if (_buffer.space() >= 2) {
              _buffer.put(HttpTokens.CRLF);
            } else {
              _needCRLF = true;
            }
          }
        }
        if ((_needEOC) && ((_content == null) || (_content.length() == 0)))
        {
          if (_needCRLF) {
            if ((_buffer == null) && (_header.space() >= 2))
            {
              _header.put(HttpTokens.CRLF);
              _needCRLF = false;
            }
            else if ((_buffer != null) && (_buffer.space() >= 2))
            {
              _buffer.put(HttpTokens.CRLF);
              _needCRLF = false;
            }
          }
          if ((!_needCRLF) && (_needEOC)) {
            if ((_buffer == null) && (_header.space() >= LAST_CHUNK.length))
            {
              if (!_head)
              {
                _header.put(LAST_CHUNK);
                _bufferChunked = true;
              }
              _needEOC = false;
            }
            else if ((_buffer != null) && (_buffer.space() >= LAST_CHUNK.length))
            {
              if (!_head)
              {
                _buffer.put(LAST_CHUNK);
                _bufferChunked = true;
              }
              _needEOC = false;
            }
          }
        }
      }
    }
    if ((_content != null) && (_content.length() == 0)) {
      _content = null;
    }
  }
  
  public int getBytesBuffered()
  {
    return (_header == null ? 0 : _header.length()) + (_buffer == null ? 0 : _buffer.length()) + (_content == null ? 0 : _content.length());
  }
  
  public boolean isEmpty()
  {
    return ((_header == null) || (_header.length() == 0)) && ((_buffer == null) || (_buffer.length() == 0)) && ((_content == null) || (_content.length() == 0));
  }
  
  public String toString()
  {
    return String.format("%s{s=%d,h=%d,b=%d,c=%d}", new Object[] { getClass().getSimpleName(), Integer.valueOf(_state), Integer.valueOf(_header == null ? -1 : _header.length()), Integer.valueOf(_buffer == null ? -1 : _buffer.length()), Integer.valueOf(_content == null ? -1 : _content.length()) });
  }
  
  private static class Status
  {
    Buffer _reason;
    Buffer _schemeCode;
    Buffer _responseLine;
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.http.HttpGenerator
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.http;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;
import org.eclipse.jetty.util.LazyList;
import org.eclipse.jetty.util.StringMap;

public class PathMap
  extends HashMap
  implements Externalizable
{
  private static String __pathSpecSeparators = ":,";
  
  public static void setPathSpecSeparators(String s)
  {
    __pathSpecSeparators = s;
  }
  
  final StringMap _prefixMap = new StringMap();
  final StringMap _suffixMap = new StringMap();
  final StringMap _exactMap = new StringMap();
  List _defaultSingletonList = null;
  Entry _prefixDefault = null;
  Entry _default = null;
  final Set _entrySet;
  boolean _nodefault = false;
  
  public PathMap()
  {
    super(11);
    _entrySet = entrySet();
  }
  
  public PathMap(boolean nodefault)
  {
    super(11);
    _entrySet = entrySet();
    _nodefault = nodefault;
  }
  
  public PathMap(int capacity)
  {
    super(capacity);
    _entrySet = entrySet();
  }
  
  public PathMap(Map m)
  {
    putAll(m);
    _entrySet = entrySet();
  }
  
  public void writeExternal(ObjectOutput out)
    throws IOException
  {
    HashMap map = new HashMap(this);
    out.writeObject(map);
  }
  
  public void readExternal(ObjectInput in)
    throws IOException, ClassNotFoundException
  {
    HashMap map = (HashMap)in.readObject();
    putAll(map);
  }
  
  public Object put(Object pathSpec, Object object)
  {
    String str = pathSpec.toString();
    if ("".equals(str.trim()))
    {
      Entry entry = new Entry("", object);
      entry.setMapped("");
      _exactMap.put("", entry);
      return super.put("", object);
    }
    StringTokenizer tok = new StringTokenizer(str, __pathSpecSeparators);
    Object old = null;
    while (tok.hasMoreTokens())
    {
      String spec = tok.nextToken();
      if ((!spec.startsWith("/")) && (!spec.startsWith("*."))) {
        throw new IllegalArgumentException("PathSpec " + spec + ". must start with '/' or '*.'");
      }
      old = super.put(spec, object);
      
      Entry entry = new Entry(spec, object);
      if (entry.getKey().equals(spec)) {
        if (spec.equals("/*"))
        {
          _prefixDefault = entry;
        }
        else if (spec.endsWith("/*"))
        {
          String mapped = spec.substring(0, spec.length() - 2);
          entry.setMapped(mapped);
          _prefixMap.put(mapped, entry);
          _exactMap.put(mapped, entry);
          _exactMap.put(spec.substring(0, spec.length() - 1), entry);
        }
        else if (spec.startsWith("*."))
        {
          _suffixMap.put(spec.substring(2), entry);
        }
        else if (spec.equals("/"))
        {
          if (_nodefault)
          {
            _exactMap.put(spec, entry);
          }
          else
          {
            _default = entry;
            _defaultSingletonList = Collections.singletonList(_default);
          }
        }
        else
        {
          entry.setMapped(spec);
          _exactMap.put(spec, entry);
        }
      }
    }
    return old;
  }
  
  public Object match(String path)
  {
    Map.Entry entry = getMatch(path);
    if (entry != null) {
      return entry.getValue();
    }
    return null;
  }
  
  public Entry getMatch(String path)
  {
    Map.Entry entry = null;
    if (path == null) {
      return null;
    }
    int l = path.length();
    if ((l == 1) && (path.charAt(0) == '/'))
    {
      entry = (Map.Entry)_exactMap.get("");
      if (entry != null) {
        return (Entry)entry;
      }
    }
    entry = _exactMap.getEntry(path, 0, l);
    if (entry != null) {
      return (Entry)entry.getValue();
    }
    int i = l;
    while ((i = path.lastIndexOf('/', i - 1)) >= 0)
    {
      entry = _prefixMap.getEntry(path, 0, i);
      if (entry != null) {
        return (Entry)entry.getValue();
      }
    }
    if (_prefixDefault != null) {
      return _prefixDefault;
    }
    i = 0;
    while ((i = path.indexOf('.', i + 1)) > 0)
    {
      entry = _suffixMap.getEntry(path, i + 1, l - i - 1);
      if (entry != null) {
        return (Entry)entry.getValue();
      }
    }
    return _default;
  }
  
  public Object getLazyMatches(String path)
  {
    Object entries = null;
    if (path == null) {
      return LazyList.getList(entries);
    }
    int l = path.length();
    
    Map.Entry entry = _exactMap.getEntry(path, 0, l);
    if (entry != null) {
      entries = LazyList.add(entries, entry.getValue());
    }
    int i = l - 1;
    while ((i = path.lastIndexOf('/', i - 1)) >= 0)
    {
      entry = _prefixMap.getEntry(path, 0, i);
      if (entry != null) {
        entries = LazyList.add(entries, entry.getValue());
      }
    }
    if (_prefixDefault != null) {
      entries = LazyList.add(entries, _prefixDefault);
    }
    i = 0;
    while ((i = path.indexOf('.', i + 1)) > 0)
    {
      entry = _suffixMap.getEntry(path, i + 1, l - i - 1);
      if (entry != null) {
        entries = LazyList.add(entries, entry.getValue());
      }
    }
    if (_default != null)
    {
      if (entries == null) {
        return _defaultSingletonList;
      }
      entries = LazyList.add(entries, _default);
    }
    return entries;
  }
  
  public List getMatches(String path)
  {
    return LazyList.getList(getLazyMatches(path));
  }
  
  public boolean containsMatch(String path)
  {
    Entry match = getMatch(path);
    return (match != null) && (!match.equals(_default));
  }
  
  public Object remove(Object pathSpec)
  {
    if (pathSpec != null)
    {
      String spec = (String)pathSpec;
      if (spec.equals("/*"))
      {
        _prefixDefault = null;
      }
      else if (spec.endsWith("/*"))
      {
        _prefixMap.remove(spec.substring(0, spec.length() - 2));
        _exactMap.remove(spec.substring(0, spec.length() - 1));
        _exactMap.remove(spec.substring(0, spec.length() - 2));
      }
      else if (spec.startsWith("*."))
      {
        _suffixMap.remove(spec.substring(2));
      }
      else if (spec.equals("/"))
      {
        _default = null;
        _defaultSingletonList = null;
      }
      else
      {
        _exactMap.remove(spec);
      }
    }
    return super.remove(pathSpec);
  }
  
  public void clear()
  {
    _exactMap.clear();
    _prefixMap.clear();
    _suffixMap.clear();
    _default = null;
    _defaultSingletonList = null;
    super.clear();
  }
  
  public static boolean match(String pathSpec, String path)
    throws IllegalArgumentException
  {
    return match(pathSpec, path, false);
  }
  
  public static boolean match(String pathSpec, String path, boolean noDefault)
    throws IllegalArgumentException
  {
    char c = pathSpec.charAt(0);
    if (c == '/')
    {
      if (((!noDefault) && (pathSpec.length() == 1)) || (pathSpec.equals(path))) {
        return true;
      }
      if (isPathWildcardMatch(pathSpec, path)) {
        return true;
      }
    }
    else if (c == '*')
    {
      return path.regionMatches(path.length() - pathSpec.length() + 1, pathSpec, 1, pathSpec.length() - 1);
    }
    return false;
  }
  
  private static boolean isPathWildcardMatch(String pathSpec, String path)
  {
    int cpl = pathSpec.length() - 2;
    if ((pathSpec.endsWith("/*")) && (path.regionMatches(0, pathSpec, 0, cpl))) {
      if ((path.length() == cpl) || ('/' == path.charAt(cpl))) {
        return true;
      }
    }
    return false;
  }
  
  public static String pathMatch(String pathSpec, String path)
  {
    char c = pathSpec.charAt(0);
    if (c == '/')
    {
      if (pathSpec.length() == 1) {
        return path;
      }
      if (pathSpec.equals(path)) {
        return path;
      }
      if (isPathWildcardMatch(pathSpec, path)) {
        return path.substring(0, pathSpec.length() - 2);
      }
    }
    else if (c == '*')
    {
      if (path.regionMatches(path.length() - (pathSpec.length() - 1), pathSpec, 1, pathSpec.length() - 1)) {
        return path;
      }
    }
    return null;
  }
  
  public static String pathInfo(String pathSpec, String path)
  {
    if ("".equals(pathSpec)) {
      return path;
    }
    char c = pathSpec.charAt(0);
    if (c == '/')
    {
      if (pathSpec.length() == 1) {
        return null;
      }
      boolean wildcard = isPathWildcardMatch(pathSpec, path);
      if ((pathSpec.equals(path)) && (!wildcard)) {
        return null;
      }
      if (wildcard)
      {
        if (path.length() == pathSpec.length() - 2) {
          return null;
        }
        return path.substring(pathSpec.length() - 2);
      }
    }
    return null;
  }
  
  public static String relativePath(String base, String pathSpec, String path)
  {
    String info = pathInfo(pathSpec, path);
    if (info == null) {
      info = path;
    }
    if (info.startsWith("./")) {
      info = info.substring(2);
    }
    if (base.endsWith("/"))
    {
      if (info.startsWith("/")) {
        path = base + info.substring(1);
      } else {
        path = base + info;
      }
    }
    else if (info.startsWith("/")) {
      path = base + info;
    } else {
      path = base + "/" + info;
    }
    return path;
  }
  
  public static class Entry
    implements Map.Entry
  {
    private final Object key;
    private final Object value;
    private String mapped;
    private transient String string;
    
    Entry(Object key, Object value)
    {
      this.key = key;
      this.value = value;
    }
    
    public Object getKey()
    {
      return key;
    }
    
    public Object getValue()
    {
      return value;
    }
    
    public Object setValue(Object o)
    {
      throw new UnsupportedOperationException();
    }
    
    public String toString()
    {
      if (string == null) {
        string = (key + "=" + value);
      }
      return string;
    }
    
    public String getMapped()
    {
      return mapped;
    }
    
    void setMapped(String mapped)
    {
      this.mapped = mapped;
    }
  }
}

/* Location:
 * Qualified Name:     org.eclipse.jetty.http.PathMap
 * Java Class Version: 6 (50.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.jetty.http;

public enum HttpStatus$Code
{
  CONTINUE(100, "Continue"),  SWITCHING_PROTOCOLS(101, "Switching Protocols"),  PROCESSING(102, "Processing"),  OK(200, "OK"),  CREATED(201, "Created"),  ACCEPTED(202, "Accepted"),  NON_AUTHORITATIVE_INFORMATION(203, "Non Authoritative Information"),  NO_CONTENT(204, "No Content"),  RESET_CONTENT(205, "Reset Content"),  PARTIAL_CONTENT(206, "Partial Content"),  MULTI_STATUS(207, "Multi-Status"),  MULTIPLE_CHOICES(300, "Multiple Choices"),  MOVED_PERMANENTLY(301, "Moved Permanently"),  MOVED_TEMPORARILY(302, "Moved Temporarily"),  FOUND(302, "Found"),  SEE_OTHER(303, "See Other"),  NOT_MODIFIED(304, "Not Modified"),  USE_PROXY(305, "Use Proxy"),  TEMPORARY_REDIRECT(307, "Temporary Redirect"),  BAD_REQUEST(400, "Bad Request"),  UNAUTHORIZED(401, "Unauthorized"),  PAYMENT_REQUIRED(402, "Payment Required"),  FORBIDDEN(403, "Forbidden"),  NOT_FOUND(404, "Not Found"),  METHOD_NOT_ALLOWED(405, "Method Not Allowed"),  NOT_ACCEPTABLE(406, "Not Acceptable"),  PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),  REQUEST_TIMEOUT(408, "Request Timeout"),  CONFLICT(409, "Conflict"),  GONE(410, "Gone"),  LENGTH_REQUIRED(411, "Length Required"),  PRECONDITION_FAILED(412, "Precondition Failed"),  REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large"),  REQUEST_URI_TOO_LONG(414, "Request-URI Too Long"),  UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),  REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested Range Not Satisfiable"),  EXPECTATION_FAILED(417, "Expectation Failed"),  UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"),  LOCKED(423, "Locked"),  FAILED_DEPENDENCY(424, "Failed Dependency"),  INTERNAL_SERVER_ERROR(500, "Server Error"),  NOT_IMPLEMENTED(501, "Not Implemented"),  BAD_GATEWAY(502, "Bad Gateway"),  SERVICE_UNAVAILABLE(503, "Service Unavailable"),  GATEWAY_TIMEOUT(504, "Gateway Timeout"),  HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported"),  INSUFFICIENT_STORAGE(507, "Insufficient Storage");
  
  private final int _code;
  private final String _message;
  
  private HttpStatus$Code(int code, String message)
  {
    _code = code;
    _message = message;
  }
  
  public int getCode()
  {
    return _code;
  }
  
  public String getMessage()
  {
    return _message;
  }
  
  public boolean equals(int code)
  {
    return _code == code;
  }
  
  public String toString()
  {
    return String.format("[%03d %s]", new Object[] { Integer.valueOf(_code), getMessage() });
  }
  
  public boolean isInformational()
  {
    return HttpStatus.isInformational(_code);
  }
  
  public boolean isSuccess()
  {
    return HttpStatus.isSuccess(_code);
  }
  
  public boolean isRedirection()
  {
    return HttpStatus.isRedirection(_code);
  }
  
  public boolean isClientError()
  {
    return HttpStatus.is
1 2 3 4 5 6

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd