Exemplo n.º 1
0
 public void sendResponseHeaders(int rCode, long contentLen) throws IOException {
   if (sentHeaders) {
     throw new IOException("headers already sent");
   }
   this.rcode = rCode;
   String statusLine = "HTTP/1.1 " + rCode + Code.msg(rCode) + "\r\n";
   OutputStream tmpout = new BufferedOutputStream(ros);
   PlaceholderOutputStream o = getPlaceholderResponseBody();
   tmpout.write(bytes(statusLine, 0), 0, statusLine.length());
   boolean noContentToSend = false; // assume there is content
   rspHdrs.set("Date", df.format(new Date()));
   if (contentLen == 0) {
     if (http10) {
       o.setWrappedStream(new UndefLengthOutputStream(this, ros));
       close = true;
     } else {
       rspHdrs.set("Transfer-encoding", "chunked");
       o.setWrappedStream(new ChunkedOutputStream(this, ros));
     }
   } else {
     if (contentLen == -1) {
       noContentToSend = true;
       contentLen = 0;
     }
     /* content len might already be set, eg to implement HEAD resp */
     if (rspHdrs.getFirst("Content-length") == null) {
       rspHdrs.set("Content-length", Long.toString(contentLen));
     }
     o.setWrappedStream(new FixedLengthOutputStream(this, ros, contentLen));
   }
   write(rspHdrs, tmpout);
   this.rspContentLen = contentLen;
   tmpout.flush();
   tmpout = null;
   sentHeaders = true;
   if (noContentToSend) {
     WriteFinishedEvent e = new WriteFinishedEvent(this);
     server.addEvent(e);
     closed = true;
   }
   server.logReply(rCode, req.requestLine(), null);
 }
Exemplo n.º 2
0
  public Set<Cookie> getCookies() {
    if (cookies == null) {
      String header = headers.get(HttpHeaderNames.COOKIE);
      if (header == null || header.length() == 0) {
        cookies = Collections.emptySet();
      } else {
        cookies = ServerCookieDecoder.STRICT.decode(header);
      }
    }

    return cookies;
  }
Exemplo n.º 3
0
 void write(Headers map, OutputStream os) throws IOException {
   Set<Map.Entry<String, List<String>>> entries = map.entrySet();
   for (Map.Entry<String, List<String>> entry : entries) {
     String key = entry.getKey();
     byte[] buf;
     List<String> values = entry.getValue();
     for (String val : values) {
       int i = key.length();
       buf = bytes(key, 2);
       buf[i++] = ':';
       buf[i++] = ' ';
       os.write(buf, 0, i);
       buf = bytes(val, 2);
       i = val.length();
       buf[i++] = '\r';
       buf[i++] = '\n';
       os.write(buf, 0, i);
     }
   }
   os.write('\r');
   os.write('\n');
 }
Exemplo n.º 4
0
 @Override
 public boolean isChunkedTransfer() {
   return Iterables.any(
       headers.getAll(HttpHeaderNames.TRANSFER_ENCODING),
       HttpHeaderValues.CHUNKED::contentEqualsIgnoreCase);
 }
Exemplo n.º 5
0
 @Override
 public boolean isExpectsContinue() {
   return Iterables.any(
       headers.getAll(HttpHeaderNames.EXPECT), HttpHeaderValues.CONTINUE::contentEqualsIgnoreCase);
 }
Exemplo n.º 6
0
 @Override
 public MediaType getContentType() {
   return DefaultMediaType.get(headers.get(HttpHeaderNames.CONTENT_TYPE));
 }
Exemplo n.º 7
0
 public boolean isAjaxRequest() {
   return HttpHeaderConstants.XML_HTTP_REQUEST.equalsIgnoreCase(
       headers.get(HttpHeaderConstants.X_REQUESTED_WITH));
 }
Exemplo n.º 8
0
 private Headers convertToHeaders(String input) {
   return Headers.fromJSON(input);
 }