All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.http.HttpParser Maven / Gradle / Ivy

// Copyright 2015-2019 SWIM.AI inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package swim.http;

import swim.codec.Decoder;
import swim.codec.Diagnostic;
import swim.codec.Input;
import swim.codec.InputBuffer;
import swim.codec.Parser;
import swim.codec.Unicode;
import swim.collections.FingerTrieSeq;
import swim.collections.HashTrieMap;
import swim.http.header.Accept;
import swim.http.header.AcceptCharset;
import swim.http.header.AcceptEncoding;
import swim.http.header.AcceptLanguage;
import swim.http.header.Allow;
import swim.http.header.Connection;
import swim.http.header.ContentEncoding;
import swim.http.header.ContentLength;
import swim.http.header.ContentType;
import swim.http.header.Expect;
import swim.http.header.Host;
import swim.http.header.MaxForwards;
import swim.http.header.Origin;
import swim.http.header.RawHeader;
import swim.http.header.SecWebSocketAccept;
import swim.http.header.SecWebSocketExtensions;
import swim.http.header.SecWebSocketKey;
import swim.http.header.SecWebSocketProtocol;
import swim.http.header.SecWebSocketVersion;
import swim.http.header.Server;
import swim.http.header.TransferEncoding;
import swim.http.header.Upgrade;
import swim.http.header.UserAgent;
import swim.uri.Uri;

public class HttpParser {
  public  HttpRequest request(HttpMethod method, Uri uri, HttpVersion version,
                                    FingerTrieSeq headers) {
    return HttpRequest.from(method, uri, version, headers);
  }

  public  HttpResponse response(HttpVersion version, HttpStatus status,
                                      FingerTrieSeq headers) {
    return HttpResponse.from(version, status, headers);
  }

  public HttpMethod method(String name) {
    return HttpMethod.from(name);
  }

  public HttpStatus status(int code, String phrase) {
    return HttpStatus.from(code, phrase);
  }

  public HttpVersion version(int major, int minor) {
    return HttpVersion.from(major, minor);
  }

  public HttpChunkHeader chunkHeader(long size, FingerTrieSeq extensions) {
    return HttpChunkHeader.from(size, extensions);
  }

  public HttpChunkTrailer chunkTrailer(FingerTrieSeq headers) {
    return HttpChunkTrailer.from(headers);
  }

  public ChunkExtension chunkExtension(String name, String value) {
    return ChunkExtension.from(name, value);
  }

  public HttpCharset charset(String name, float weight) {
    return HttpCharset.from(name, weight);
  }

  public LanguageRange languageRange(String tag, String subtag, float weight) {
    return LanguageRange.from(tag, subtag, weight);
  }

  public MediaRange mediaRange(String type, String subtype, float weight, HashTrieMap params) {
    return MediaRange.from(type, subtype, weight, params);
  }

  public MediaType mediaType(String type, String subtype, HashTrieMap params) {
    return MediaType.from(type, subtype, params);
  }

  public Product product(String name, String version, FingerTrieSeq comments) {
    return Product.from(name, version, comments);
  }

  public ContentCoding contentCoding(String name, float weight) {
    return ContentCoding.from(name, weight);
  }

  public TransferCoding transferCoding(String name, HashTrieMap params) {
    return TransferCoding.from(name, params);
  }

  public UpgradeProtocol upgradeProtocol(String name, String version) {
    return UpgradeProtocol.from(name, version);
  }

  public WebSocketParam webSocketParam(String key, String value) {
    return WebSocketParam.from(key, value);
  }

  public WebSocketExtension webSocketExtension(String name, FingerTrieSeq params) {
    return WebSocketExtension.from(name, params);
  }

  public  Parser> requestParser() {
    return new HttpRequestParser(this);
  }

  public  Parser> parseRequest(Input input) {
    return HttpRequestParser.parse(input, this);
  }

  public  HttpRequest parseRequestString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser> parser = parseRequest(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public  Parser> responseParser() {
    return new HttpResponseParser(this);
  }

  public  Parser> parseResponse(Input input) {
    return HttpResponseParser.parse(input, this);
  }

  public  HttpResponse parseResponseString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser> parser = parseResponse(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser methodParser() {
    return new HttpMethodParser(this);
  }

  public Parser parseMethod(Input input) {
    return HttpMethodParser.parse(input, this);
  }

  public HttpMethod parseMethodString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseMethod(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser statusParser() {
    return new HttpStatusParser(this);
  }

  public Parser parseStatus(Input input) {
    return HttpStatusParser.parse(input, this);
  }

  public HttpStatus parseStatusString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseStatus(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser versionParser() {
    return new HttpVersionParser(this);
  }

  public Parser parseVersion(Input input) {
    return HttpVersionParser.parse(input, this);
  }

  public HttpVersion parseVersionString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseVersion(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser headerParser() {
    return new HttpHeaderParser(this);
  }

  public Parser parseHeader(Input input) {
    return HttpHeaderParser.parse(input, this);
  }

  public HttpHeader parseHeaderString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseHeader(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser parseHeaderValue(String name, Input input) {
    if ("Accept".equalsIgnoreCase(name)) {
      return Accept.parseHttpValue(input, this);
    } else if ("Accept-Charset".equalsIgnoreCase(name)) {
      return AcceptCharset.parseHttpValue(input, this);
    } else if ("Accept-Encoding".equalsIgnoreCase(name)) {
      return AcceptEncoding.parseHttpValue(input, this);
    } else if ("Accept-Language".equalsIgnoreCase(name)) {
      return AcceptLanguage.parseHttpValue(input, this);
    } else if ("Allow".equalsIgnoreCase(name)) {
      return Allow.parseHttpValue(input, this);
    } else if ("Connection".equalsIgnoreCase(name)) {
      return Connection.parseHttpValue(input, this);
    } else if ("Content-Encoding".equalsIgnoreCase(name)) {
      return ContentEncoding.parseHttpValue(input, this);
    } else if ("Content-Length".equalsIgnoreCase(name)) {
      return ContentLength.parseHttpValue(input, this);
    } else if ("Content-Type".equalsIgnoreCase(name)) {
      return ContentType.parseHttpValue(input, this);
    } else if ("Expect".equalsIgnoreCase(name)) {
      return Expect.parseHttpValue(input, this);
    } else if ("Host".equalsIgnoreCase(name)) {
      return Host.parseHttpValue(input, this);
    } else if ("Max-Forwards".equalsIgnoreCase(name)) {
      return MaxForwards.parseHttpValue(input, this);
    } else if ("Origin".equalsIgnoreCase(name)) {
      return Origin.parseHttpValue(input, this);
    } else if ("Sec-WebSocket-Accept".equalsIgnoreCase(name)) {
      return SecWebSocketAccept.parseHttpValue(input, this);
    } else if ("Sec-WebSocket-Extensions".equalsIgnoreCase(name)) {
      return SecWebSocketExtensions.parseHttpValue(input, this);
    } else if ("Sec-WebSocket-Key".equalsIgnoreCase(name)) {
      return SecWebSocketKey.parseHttpValue(input, this);
    } else if ("Sec-WebSocket-Protocol".equalsIgnoreCase(name)) {
      return SecWebSocketProtocol.parseHttpValue(input, this);
    } else if ("Sec-WebSocket-Version".equalsIgnoreCase(name)) {
      return SecWebSocketVersion.parseHttpValue(input, this);
    } else if ("Server".equals(name)) {
      return Server.parseHttpValue(input, this);
    } else if ("Transfer-Encoding".equalsIgnoreCase(name)) {
      return TransferEncoding.parseHttpValue(input, this);
    } else if ("Upgrade".equalsIgnoreCase(name)) {
      return Upgrade.parseHttpValue(input, this);
    } else if ("User-Agent".equalsIgnoreCase(name)) {
      return UserAgent.parseHttpValue(input, this);
    } else {
      return RawHeader.parseHttpValue(input, this, name.toLowerCase(), name);
    }
  }

  public Parser chunkHeaderParser() {
    return new HttpChunkHeaderParser(this);
  }

  public Parser parseChunkHeader(Input input) {
    return HttpChunkHeaderParser.parse(input, this);
  }

  public HttpChunkHeader parseChunkHeaderString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseChunkHeader(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser chunkTrailerParser() {
    return new HttpChunkTrailerParser(this);
  }

  public Parser parseChunkTrailer(Input input) {
    return HttpChunkTrailerParser.parse(input, this);
  }

  public HttpChunkTrailer parseChunkTrailerString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseChunkTrailer(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser chunkExtensionParser() {
    return new ChunkExtensionParser(this);
  }

  public Parser parseChunkExtension(Input input) {
    return ChunkExtensionParser.parse(input, this);
  }

  public ChunkExtension parseChunkExtensionString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseChunkExtension(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser charsetParser() {
    return new HttpCharsetParser(this);
  }

  public Parser parseCharset(Input input) {
    return HttpCharsetParser.parse(input, this);
  }

  public HttpCharset parseCharsetString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseCharset(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser languageRangeParser() {
    return new LanguageRangeParser(this);
  }

  public Parser parseLanguageRange(Input input) {
    return LanguageRangeParser.parse(input, this);
  }

  public LanguageRange parseLanguageRangeString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseLanguageRange(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser mediaRangeParser() {
    return new MediaRangeParser(this);
  }

  public Parser parseMediaRange(Input input) {
    return MediaRangeParser.parse(input, this);
  }

  public MediaRange parseMediaRangeString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseMediaRange(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser mediaTypeParser() {
    return new MediaTypeParser(this);
  }

  public Parser parseMediaType(Input input) {
    return MediaTypeParser.parse(input, this);
  }

  public MediaType parseMediaTypeString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseMediaType(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser productParser() {
    return new ProductParser(this);
  }

  public Parser parseProduct(Input input) {
    return ProductParser.parse(input, this);
  }

  public Product parseProductString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseProduct(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser contentCodingParser() {
    return new ContentCodingParser(this);
  }

  public Parser parseContentCoding(Input input) {
    return ContentCodingParser.parse(input, this);
  }

  public ContentCoding parseContentCodingString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseContentCoding(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser transferCodingParser() {
    return new TransferCodingParser(this);
  }

  public Parser parseTransferCoding(Input input) {
    return TransferCodingParser.parse(input, this);
  }

  public TransferCoding parseTransferCodingString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseTransferCoding(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser upgradeProtocolParser() {
    return new UpgradeProtocolParser(this);
  }

  public Parser parseUpgradeProtocol(Input input) {
    return UpgradeProtocolParser.parse(input, this);
  }

  public UpgradeProtocol parseUpgradeProtocolString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseUpgradeProtocol(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser webSocketParamParser() {
    return new WebSocketParamParser(this);
  }

  public Parser parseWebSocketParam(Input input) {
    return WebSocketParamParser.parse(input, this);
  }

  public WebSocketParam parseWebSocketParamString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseWebSocketParam(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser webSocketExtensionParser() {
    return new WebSocketExtensionParser(this);
  }

  public Parser parseWebSocketExtension(Input input) {
    return WebSocketExtensionParser.parse(input, this);
  }

  public WebSocketExtension parseWebSocketExtensionString(String string) {
    final Input input = Unicode.stringInput(string);
    Parser parser = parseWebSocketExtension(input);
    if (input.isCont() && !parser.isError()) {
      parser = Parser.error(Diagnostic.unexpected(input));
    }
    return parser.bind();
  }

  public Parser parseQValue(Input input) {
    return QValueParser.parse(input);
  }

  public Parser parseQValueRest(Input input) {
    return QValueParser.parseRest(input);
  }

  public Parser parseComment(Input input) {
    return CommentParser.parse(input);
  }

  public Parser> parseTokenList(Input input) {
    return TokenListParser.parse(input);
  }

  public Parser> parseParamMap(Input input) {
    return ParamMapParser.parse(input);
  }

  public Parser> parseParamMapRest(Input input) {
    return ParamMapParser.parseRest(input);
  }

  public Parser> parseParamMapRest(StringBuilder key, Input input) {
    return ParamMapParser.parseRest(input, key);
  }

  public  Decoder> bodyDecoder(HttpMessage message, Decoder content, long length) {
    return new HttpBodyDecoder(message, content, length);
  }

  public  Decoder> decodeBody(HttpMessage message, Decoder content,
                                                long length, InputBuffer input) {
    return HttpBodyDecoder.decode(input, message, content, length);
  }

  public  Decoder> chunkedDecoder(HttpMessage message, Decoder content) {
    return new HttpChunkedDecoder(this, message, content);
  }

  public  Decoder> decodeChunked(HttpMessage message, Decoder content,
                                                   InputBuffer input) {
    return HttpChunkedDecoder.decode(input, this, message, content);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy