use of org.apache.http.ProtocolVersion in project XobotOS by xamarin.
the class ResponseContent method process.
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
if (response == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (response.containsHeader(HTTP.TRANSFER_ENCODING)) {
throw new ProtocolException("Transfer-encoding header already present");
}
if (response.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}
ProtocolVersion ver = response.getStatusLine().getProtocolVersion();
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (entity.isChunked() && !ver.lessEquals(HttpVersion.HTTP_1_0)) {
response.addHeader(HTTP.TRANSFER_ENCODING, HTTP.CHUNK_CODING);
} else if (len >= 0) {
response.addHeader(HTTP.CONTENT_LEN, Long.toString(entity.getContentLength()));
}
// Specify a content type if known
if (entity.getContentType() != null && !response.containsHeader(HTTP.CONTENT_TYPE)) {
response.addHeader(entity.getContentType());
}
// Specify a content encoding if known
if (entity.getContentEncoding() != null && !response.containsHeader(HTTP.CONTENT_ENCODING)) {
response.addHeader(entity.getContentEncoding());
}
} else {
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_NOT_MODIFIED && status != HttpStatus.SC_RESET_CONTENT) {
response.addHeader(HTTP.CONTENT_LEN, "0");
}
}
}
use of org.apache.http.ProtocolVersion in project XobotOS by xamarin.
the class BasicLineParser method parseRequestLine.
/**
* Parses a request line.
*
* @param buffer a buffer holding the line to parse
*
* @return the parsed request line
*
* @throws ParseException in case of a parse error
*/
public RequestLine parseRequestLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
if (buffer == null) {
throw new IllegalArgumentException("Char array buffer may not be null");
}
if (cursor == null) {
throw new IllegalArgumentException("Parser cursor may not be null");
}
int indexFrom = cursor.getPos();
int indexTo = cursor.getUpperBound();
try {
skipWhitespace(buffer, cursor);
int i = cursor.getPos();
int blank = buffer.indexOf(' ', i, indexTo);
if (blank < 0) {
throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
}
String method = buffer.substringTrimmed(i, blank);
cursor.updatePos(blank);
skipWhitespace(buffer, cursor);
i = cursor.getPos();
blank = buffer.indexOf(' ', i, indexTo);
if (blank < 0) {
throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
}
String uri = buffer.substringTrimmed(i, blank);
cursor.updatePos(blank);
ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
skipWhitespace(buffer, cursor);
if (!cursor.atEnd()) {
throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
}
return createRequestLine(method, uri, ver);
} catch (IndexOutOfBoundsException e) {
throw new ParseException("Invalid request line: " + buffer.substring(indexFrom, indexTo));
}
}
use of org.apache.http.ProtocolVersion in project XobotOS by xamarin.
the class BasicLineParser method parseStatusLine.
// non-javadoc, see interface LineParser
public StatusLine parseStatusLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
if (buffer == null) {
throw new IllegalArgumentException("Char array buffer may not be null");
}
if (cursor == null) {
throw new IllegalArgumentException("Parser cursor may not be null");
}
int indexFrom = cursor.getPos();
int indexTo = cursor.getUpperBound();
try {
// handle the HTTP-Version
ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
// handle the Status-Code
skipWhitespace(buffer, cursor);
int i = cursor.getPos();
int blank = buffer.indexOf(' ', i, indexTo);
if (blank < 0) {
blank = indexTo;
}
int statusCode = 0;
try {
statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
} catch (NumberFormatException e) {
throw new ParseException("Unable to parse status code from status line: " + buffer.substring(indexFrom, indexTo));
}
//handle the Reason-Phrase
i = blank;
String reasonPhrase = null;
if (i < indexTo) {
reasonPhrase = buffer.substringTrimmed(i, indexTo);
} else {
reasonPhrase = "";
}
return createStatusLine(ver, statusCode, reasonPhrase);
} catch (IndexOutOfBoundsException e) {
throw new ParseException("Invalid status line: " + buffer.substring(indexFrom, indexTo));
}
}
use of org.apache.http.ProtocolVersion in project XobotOS by xamarin.
the class BasicHttpResponse method setStatusCode.
// non-javadoc, see interface HttpResponse
public void setStatusCode(int code) {
// argument checked in BasicStatusLine constructor
ProtocolVersion ver = this.statusline.getProtocolVersion();
this.statusline = new BasicStatusLine(ver, code, getReason(code));
}
use of org.apache.http.ProtocolVersion in project XobotOS by xamarin.
the class HttpRequestBase method getRequestLine.
public RequestLine getRequestLine() {
String method = getMethod();
ProtocolVersion ver = getProtocolVersion();
URI uri = getURI();
String uritext = null;
if (uri != null) {
uritext = uri.toASCIIString();
}
if (uritext == null || uritext.length() == 0) {
uritext = "/";
}
return new BasicRequestLine(method, uritext, ver);
}
Aggregations