private static void printHeaders(CharacterHttpResponse response) { System.out.println(response.getStatusLine()); for (HttpHeader next : response.getHeaders()) { System.out.printf("%s: %s\n", next.getName(), next.getValue()); } }
/** Sets the {@code "Date"} header. */ public static void setDate(HttpHeader message, Date value) { if (value != null) { message.setHeader(Names.DATE, new HttpHeaderDateFormat().format(value)); } else { message.setHeader(Names.DATE, null); } }
/** * Returns {@code true} if and only if the specified message contains the {@code "Expect: * 100-continue"} header. */ public static boolean is100ContinueExpected(HttpHeader message) { // Expect: 100-continue is for requests only. if (!(message instanceof HttpRequestHeader)) { return false; } // It works only on HTTP/1.1 or later. if (message.getProtocolVersion().compareTo(HttpVersion.HTTP_1_1) < 0) { return false; } // In most cases, there will be one or zero 'Expect' header. String value = message.getHeader(Names.EXPECT); if (value == null) { return false; } if (Values.CONTINUE.equalsIgnoreCase(value)) { return true; } // Multiple 'Expect' headers. Search through them. for (String v : message.getHeaders(Names.EXPECT)) { if (Values.CONTINUE.equalsIgnoreCase(v)) { return true; } } return false; }
/** * Sets or removes the {@code "Expect: 100-continue"} header to / from the specified message. If * the specified {@code value} is {@code true}, the {@code "Expect: 100-continue"} header is set * and all other previous {@code "Expect"} headers are removed. Otherwise, all {@code "Expect"} * headers are removed completely. */ public static void set100ContinueExpected(HttpHeader message, boolean set) { if (set) { message.setHeader(Names.EXPECT, Values.CONTINUE); } else { message.removeHeader(Names.EXPECT); } }
/** * Sets a new date header with the specified name and value. If there is an existing header with * the same name, the existing header is removed. The specified value is formatted as defined in * <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1">RFC2616</a> */ public static void setDateHeader(HttpHeader message, String name, Date value) { if (value != null) { message.setHeader(name, new HttpHeaderDateFormat().format(value)); } else { message.setHeader(name, null); } }
private static String getLocationFromHeader(CharacterHttpResponse response) { for (HttpHeader next : response.getHeaders()) { if (next.getName().contains("Location")) { return next.getValue(); } } return null; }
public static void removeTransferEncodingChunked(HttpHeader m) { List<String> values = m.getHeaders(Names.TRANSFER_ENCODING); values.remove(Values.CHUNKED); if (values.isEmpty()) { m.removeHeader(Names.TRANSFER_ENCODING); } else { m.setHeader(Names.TRANSFER_ENCODING, values); } }
/** * Returns {@code true} if and only if the connection can remain open and thus 'kept alive'. This * methods respects the value of the {@code "Connection"} header first and then the return value * of {@link HttpVersion#isKeepAliveDefault()}. */ public static boolean isKeepAlive(HttpHeader message) { String connection = message.getHeader(Names.CONNECTION); if (Values.CLOSE.equalsIgnoreCase(connection)) { return false; } if (message.getProtocolVersion().isKeepAliveDefault()) { return !Values.CLOSE.equalsIgnoreCase(connection); } else { return Values.KEEP_ALIVE.equalsIgnoreCase(connection); } }
@Before public void setUp() { factory = new HeaderNormalizationHandlerFactory(null); headerFilterList = new HeaderFilterList(); whitelist = new HttpHeaderList(); header1 = new HttpHeader(); header3 = new HttpHeader(); header2 = new HttpHeader(); config = new HeaderNormalizationConfig(); target = new Target(); header1.setId(hv1); whitelist.getHeader().add(header1); header2.setId(hv2); whitelist.getHeader().add(header2); header3.setId(hv3); whitelist.getHeader().add(header3); target.getWhitelist().add(whitelist); target.setUriRegex(uriRegex); target.getHttpMethods().add(HttpMethod.GET); headerFilterList.getTarget().add(target); config.setHeaderFilters(headerFilterList); factory.configurationUpdated(config); handler = factory.buildHandler(); request = mock(HttpServletRequest.class); Enumeration<String> e = new Enumeration<String>() { int size = Array.getLength(requestHeaders); int cursor; @Override public boolean hasMoreElements() { return (cursor < size); } @Override public String nextElement() { return (String) Array.get(requestHeaders, cursor++); } }; when(request.getHeaderNames()).thenReturn(e); }
/** * Sets the value of the {@code "Connection"} header depending on the protocol version of the * specified message. This method sets or removes the {@code "Connection"} header depending on * what the default keep alive mode of the message's protocol version is, as specified by {@link * HttpVersion#isKeepAliveDefault()}. * * <ul> * <li>If the connection is kept alive by default: * <ul> * <li>set to {@code "close"} if {@code keepAlive} is {@code false}. * <li>remove otherwise. * </ul> * <li>If the connection is closed by default: * <ul> * <li>set to {@code "keep-alive"} if {@code keepAlive} is {@code true}. * <li>remove otherwise. * </ul> * </ul> */ public static void setKeepAlive(HttpHeader message, boolean keepAlive) { if (message.getProtocolVersion().isKeepAliveDefault()) { if (keepAlive) { message.removeHeader(Names.CONNECTION); } else { message.setHeader(Names.CONNECTION, Values.CLOSE); } } else { if (keepAlive) { message.setHeader(Names.CONNECTION, Values.KEEP_ALIVE); } else { message.removeHeader(Names.CONNECTION); } } }
/** * Returns the header value with the specified header name. If there are more than one header * value for the specified header name, the first value is returned. * * @return the header value or the {@code defaultValue} if there is no such header */ public static String getHeader(HttpHeader message, String name, String defaultValue) { String value = message.getHeader(name); if (value == null) { return defaultValue; } return value; }
public static void putTo(HttpField field, ByteBuffer bufferInFillMode) { if (field instanceof PreEncodedHttpField) { ((PreEncodedHttpField) field).putTo(bufferInFillMode, HttpVersion.HTTP_1_0); } else { HttpHeader header = field.getHeader(); if (header != null) { bufferInFillMode.put(header.getBytesColonSpace()); putSanitisedValue(field.getValue(), bufferInFillMode); } else { putSanitisedName(field.getName(), bufferInFillMode); bufferInFillMode.put(__colon_space); putSanitisedValue(field.getValue(), bufferInFillMode); } BufferUtil.putCRLF(bufferInFillMode); } }
private Response parseResponse(BufferedReader in) throws IOException { final Response result = new Response(); result.setStatusLine(in.readLine()); String next; while (!(next = in.readLine()).equals("")) { result.getHeaders().add(HttpHeader.createFromHeaderLine(next)); } in.read(result.getBody()); return result; }
/** * Checks to see if the transfer encoding in a specified {@link HttpHeader} is chunked * * @param message The message to check * @return True if transfer encoding is chunked, otherwise false */ public static boolean isTransferEncodingChunked(HttpHeader message) { List<String> transferEncodingHeaders = message.getHeaders(Names.TRANSFER_ENCODING); if (transferEncodingHeaders.isEmpty()) { return false; } for (String value : transferEncodingHeaders) { if (value.equalsIgnoreCase(Values.CHUNKED)) { return true; } } return false; }
@Override @SuppressWarnings("unchecked") public HttpHeader header(String key) { List<String> headerNames = list(request.getHeaderNames()); for (String currentKey : headerNames) { if (currentKey.toLowerCase().equals(key.toLowerCase())) { List<String> valueList = list(request.getHeaders(currentKey)); return new HttpHeader(key, valueList); } } return HttpHeader.absent(key); }
/** {@inheritDoc} */ @Override protected void reset() { httpStatus = null; acknowledgment = false; allowCustomReasonPhrase = true; isHtmlEncodingCustomReasonPhrase = true; reasonPhraseC.recycle(); locale = null; contentLanguage = null; request = null; super.reset(); }
/** * Opens the connection to the server, then sets the header. Builds the body after. * * @return Request for further action. */ @Override public HttpPostRequest form() { try { if (this.connection == null) this.connection = (HttpURLConnection) url.openConnection(); for (Map.Entry<String, String> headerEntry : header.getContents().entrySet()) connection.setRequestProperty(headerEntry.getKey(), headerEntry.getValue()); buildBody(); } catch (IOException e) { e.printStackTrace(); } return this; }
private CharacterHttpResponse parseResponse(BufferedReader in) throws IOException { final CharacterHttpResponse result = new CharacterHttpResponse(); result.setStatusLine(in.readLine()); String next; while (!(next = in.readLine()).equals("")) { result.getHeaders().add(HttpHeader.createFromHeaderLine(next)); } // TODO chunked transfer-encoding is not supported! // reading body - we already know how many bytes the body is // (from the content-length header line) in.read(result.getBody()); return result; }
private HttpHeaders headers(HttpServletRequest request) { List<HttpHeader> headers = new LinkedList<>(); Enumeration<String> keys = request.getHeaderNames(); while (keys.hasMoreElements()) { String key = keys.nextElement(); Enumeration<String> strings = request.getHeaders(key); List<HttpHeaderValue> values = new LinkedList<>(); while (strings.hasMoreElements()) { values.add(new HttpHeaderValue(strings.nextElement())); } HttpHeader header = HttpHeader.create(key, values); if (header != null) { headers.add(header); } } return new HttpHeaders(headers); }
/** * Returns the length of the content. Please note that this value is not retrieved from {@link * HttpContent#getContent()} but from the {@code "Content-Length"} header, and thus they are * independent from each other. * * @return the content length or {@code defaultValue} if this message does not have the {@code * "Content-Length"} header or its value is not a number */ public static long getContentLength(HttpHeader message, long defaultValue) { String contentLength = message.getHeader(Names.CONTENT_LENGTH); if (contentLength != null) { try { return Long.parseLong(contentLength); } catch (NumberFormatException e) { return defaultValue; } } // We know the content length if it's a Web Socket message even if // Content-Length header is missing. long webSocketContentLength = getWebSocketContentLength(message); if (webSocketContentLength >= 0) { return webSocketContentLength; } // Otherwise we don't. return defaultValue; }
/** Returns the value of the {@code "Host"} header. */ public static String getHost(HttpHeader message) { return message.getHeader(Names.HOST); }
public LongValueHttpField(HttpHeader header, long value) { this(header, header.asString(), value); }
/** Sets the {@code "Host"} header. */ public static void setHost(HttpHeader message, String value) { message.setHeader(Names.HOST, value); }
public IntValueHttpField(HttpHeader header, int value) { this(header, header.asString(), value); }
public HttpField(HttpHeader header, HttpHeaderValue value) { this(header, header.asString(), value.asString()); }
public IntValueHttpField(HttpHeader header, String value, int intValue) { this(header, header.asString(), value, Integer.valueOf(value)); }
public HttpField(HttpHeader header, String value) { this(header, header.asString(), value); }
@Override public int hashCode() { if (_header == null) return _value.hashCode() ^ nameHashCode(); return _value.hashCode() ^ _header.hashCode(); }
/** Set the headers on the dst like they are set on the src */ public static void setHeaders(HttpHeader src, HttpHeader dst) { for (String name : src.getHeaderNames()) { dst.setHeader(name, src.getHeaders(name)); } }
public static boolean isContentLengthSet(HttpHeader m) { List<String> contentLength = m.getHeaders(Names.CONTENT_LENGTH); return !contentLength.isEmpty(); }