use of org.webpieces.httpparser.api.dto.HttpVersion in project webpieces by deanhiller.
the class ParserUtil method parseVersion.
public HttpVersion parseVersion(String versionString, String firstLine) {
if (!versionString.startsWith("HTTP/")) {
throw new ParseException("Invalid version in http request first line not prefixed with HTTP/. line=" + firstLine);
}
String ver = versionString.substring(5, versionString.length());
HttpVersion version = new HttpVersion();
version.setVersion(ver);
return version;
}
use of org.webpieces.httpparser.api.dto.HttpVersion in project webpieces by deanhiller.
the class Translations2 method translate.
public static HttpFullRequest translate(FullRequest request) {
// String scheme = request.getHeaders().getScheme();
Http2Request headers = request.getHeaders();
String authority = headers.getAuthority();
String path = headers.getPath();
String methodString = headers.getMethodString();
if (methodString == null)
throw new IllegalArgumentException("http2 :method header is required");
else if (authority == null) {
throw new IllegalArgumentException("http1 required host header so http2 message must have :authority header set");
}
HttpRequestLine reqLine = new HttpRequestLine();
reqLine.setUri(new HttpUri(path));
reqLine.setMethod(new HttpRequestMethod(methodString));
reqLine.setVersion(new HttpVersion());
HttpRequest httpReq = new HttpRequest();
httpReq.setRequestLine(reqLine);
DataWrapper data = request.getPayload();
// translate all other headers here as well...
for (Http2Header header : headers.getHeaders()) {
if (// All standard headers go elsewhere except HOST which we do below
!header.getName().startsWith(":"))
httpReq.addHeader(new Header(header.getName(), header.getValue()));
}
httpReq.addHeader(new Header(KnownHeaderName.HOST, authority));
HttpFullRequest req = new HttpFullRequest(httpReq, data);
return req;
}
use of org.webpieces.httpparser.api.dto.HttpVersion in project webpieces by deanhiller.
the class HttpParserImpl method parseRequest.
private HttpMessage parseRequest(MementoImpl memento, List<String> lines) {
// remove first line...
String firstLine = lines.remove(0);
String[] firstLinePieces = firstLine.split("\\s+");
if (firstLinePieces.length != 3) {
throw new ParseException("Unable to parse invalid http request due to first line being invalid=" + firstLine + " all Lines=" + lines);
}
HttpRequestMethod method = new HttpRequestMethod(firstLinePieces[0]);
HttpUri uri = new HttpUri(firstLinePieces[1]);
HttpVersion version = parserUtil.parseVersion(firstLinePieces[2], firstLine);
HttpRequestLine httpRequestLine = new HttpRequestLine();
httpRequestLine.setMethod(method);
httpRequestLine.setUri(uri);
httpRequestLine.setVersion(version);
HttpRequest request = new HttpRequest();
request.setRequestLine(httpRequestLine);
parserUtil.parseHeaders(lines, request);
memento.addMessage(request);
return request;
}
use of org.webpieces.httpparser.api.dto.HttpVersion in project webpieces by deanhiller.
the class HttpParserImpl method parseResponse.
private HttpMessage parseResponse(MementoImpl memento, List<String> lines) {
// remove first line...
String firstLine = lines.remove(0);
// In the case of response, a reason may contain spaces so we must split on first and second
// whitespace only
int indexOf = firstLine.indexOf(" ");
if (indexOf < 0)
throw new IllegalArgumentException("The first line of http request is invalid=" + firstLine);
String versionStr = firstLine.substring(0, indexOf).trim();
String tail = firstLine.substring(indexOf).trim();
int indexOf2 = tail.indexOf(" ");
if (indexOf2 < 0)
throw new IllegalArgumentException("The first line of http request is invalid=" + firstLine);
String codeStr = tail.substring(0, indexOf2).trim();
String reason = tail.substring(indexOf2).trim();
HttpVersion version2 = parserUtil.parseVersion(versionStr, firstLine);
HttpResponseStatus status = new HttpResponseStatus();
Integer codeVal = toInteger(codeStr, firstLine);
if (codeVal <= 0 || codeVal >= 1000)
throw new IllegalArgumentException("invalid status code. response line=" + firstLine);
status.setCode(codeVal);
status.setReason(reason);
HttpResponseStatusLine httpRequestLine = new HttpResponseStatusLine();
httpRequestLine.setStatus(status);
httpRequestLine.setVersion(version2);
HttpResponse response = new HttpResponse();
response.setStatusLine(httpRequestLine);
parserUtil.parseHeaders(lines, response);
memento.addMessage(response);
return response;
}
use of org.webpieces.httpparser.api.dto.HttpVersion in project webpieces by deanhiller.
the class HttpParserImpl method parseVersion.
private HttpVersion parseVersion(String versionString, String firstLine) {
if (!versionString.startsWith("HTTP/")) {
throw new ParseException("Invalid version in http request first line not prefixed with HTTP/. line=" + firstLine);
}
String ver = versionString.substring(5, versionString.length());
HttpVersion version = new HttpVersion();
version.setVersion(ver);
return version;
}
Aggregations