use of org.apache.http.HeaderElement in project XobotOS by xamarin.
the class LaxContentLengthStrategy method determineLength.
public long determineLength(final HttpMessage message) throws HttpException {
if (message == null) {
throw new IllegalArgumentException("HTTP message may not be null");
}
HttpParams params = message.getParams();
boolean strict = params.isParameterTrue(CoreProtocolPNames.STRICT_TRANSFER_ENCODING);
Header transferEncodingHeader = message.getFirstHeader(HTTP.TRANSFER_ENCODING);
Header contentLengthHeader = message.getFirstHeader(HTTP.CONTENT_LEN);
// RFC2616, 4.4 item number 3
if (transferEncodingHeader != null) {
HeaderElement[] encodings = null;
try {
encodings = transferEncodingHeader.getElements();
} catch (ParseException px) {
throw new ProtocolException("Invalid Transfer-Encoding header value: " + transferEncodingHeader, px);
}
if (strict) {
// Currently only chunk and identity are supported
for (int i = 0; i < encodings.length; i++) {
String encoding = encodings[i].getName();
if (encoding != null && encoding.length() > 0 && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING) && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
throw new ProtocolException("Unsupported transfer encoding: " + encoding);
}
}
}
// The chunked encoding must be the last one applied RFC2616, 14.41
int len = encodings.length;
if (HTTP.IDENTITY_CODING.equalsIgnoreCase(transferEncodingHeader.getValue())) {
return IDENTITY;
} else if ((len > 0) && (HTTP.CHUNK_CODING.equalsIgnoreCase(encodings[len - 1].getName()))) {
return CHUNKED;
} else {
if (strict) {
throw new ProtocolException("Chunk-encoding must be the last one applied");
}
return IDENTITY;
}
} else if (contentLengthHeader != null) {
long contentlen = -1;
Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
if (strict && headers.length > 1) {
throw new ProtocolException("Multiple content length headers");
}
for (int i = headers.length - 1; i >= 0; i--) {
Header header = headers[i];
try {
contentlen = Long.parseLong(header.getValue());
break;
} catch (NumberFormatException e) {
if (strict) {
throw new ProtocolException("Invalid content length: " + header.getValue());
}
}
// See if we can have better luck with another header, if present
}
if (contentlen >= 0) {
return contentlen;
} else {
return IDENTITY;
}
} else {
return IDENTITY;
}
}
use of org.apache.http.HeaderElement in project XobotOS by xamarin.
the class CookieSpecBase method parse.
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException {
List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
for (HeaderElement headerelement : elems) {
String name = headerelement.getName();
String value = headerelement.getValue();
if (name == null || name.length() == 0) {
throw new MalformedCookieException("Cookie name may not be empty");
}
BasicClientCookie cookie = new BasicClientCookie(name, value);
cookie.setPath(getDefaultPath(origin));
cookie.setDomain(getDefaultDomain(origin));
// cycle through the parameters
NameValuePair[] attribs = headerelement.getParameters();
for (int j = attribs.length - 1; j >= 0; j--) {
NameValuePair attrib = attribs[j];
String s = attrib.getName().toLowerCase(Locale.ENGLISH);
cookie.setAttribute(s, attrib.getValue());
CookieAttributeHandler handler = findAttribHandler(s);
if (handler != null) {
handler.parse(cookie, attrib.getValue());
}
}
cookies.add(cookie);
}
return cookies;
}
use of org.apache.http.HeaderElement in project robovm by robovm.
the class HttpOptions method getAllowedMethods.
public Set<String> getAllowedMethods(final HttpResponse response) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
HeaderIterator it = response.headerIterator("Allow");
Set<String> methods = new HashSet<String>();
while (it.hasNext()) {
Header header = it.nextHeader();
HeaderElement[] elements = header.getElements();
for (HeaderElement element : elements) {
methods.add(element.getName());
}
}
return methods;
}
use of org.apache.http.HeaderElement in project robovm by robovm.
the class RFC2617Scheme method parseChallenge.
@Override
protected void parseChallenge(final CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
HeaderValueParser parser = BasicHeaderValueParser.DEFAULT;
ParserCursor cursor = new ParserCursor(pos, buffer.length());
HeaderElement[] elements = parser.parseElements(buffer, cursor);
if (elements.length == 0) {
throw new MalformedChallengeException("Authentication challenge is empty");
}
this.params = new HashMap<String, String>(elements.length);
for (HeaderElement element : elements) {
this.params.put(element.getName(), element.getValue());
}
}
use of org.apache.http.HeaderElement in project robovm by robovm.
the class BrowserCompatSpec method parse.
public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException {
if (header == null) {
throw new IllegalArgumentException("Header may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
String headervalue = header.getValue();
boolean isNetscapeCookie = false;
int i1 = headervalue.toLowerCase(Locale.ENGLISH).indexOf("expires=");
if (i1 != -1) {
i1 += "expires=".length();
int i2 = headervalue.indexOf(';', i1);
if (i2 == -1) {
i2 = headervalue.length();
}
try {
DateUtils.parseDate(headervalue.substring(i1, i2), this.datepatterns);
isNetscapeCookie = true;
} catch (DateParseException e) {
// Does not look like a valid expiry date
}
}
HeaderElement[] elems = null;
if (isNetscapeCookie) {
NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT;
CharArrayBuffer buffer;
ParserCursor cursor;
if (header instanceof FormattedHeader) {
buffer = ((FormattedHeader) header).getBuffer();
cursor = new ParserCursor(((FormattedHeader) header).getValuePos(), buffer.length());
} else {
String s = header.getValue();
if (s == null) {
throw new MalformedCookieException("Header value is null");
}
buffer = new CharArrayBuffer(s.length());
buffer.append(s);
cursor = new ParserCursor(0, buffer.length());
}
elems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
} else {
elems = header.getElements();
}
return parse(elems, origin);
}
Aggregations