use of org.apache.commons.httpclient.Header in project zaproxy by zaproxy.
the class ZapHttpParser method parseHeaders.
/*
* Implementation copied from HttpParser#parseHeaders(InputStream, String) except that no exception is thrown in case of
* malformed HTTP header lines.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "null" })
public static Header[] parseHeaders(InputStream is, String charset) throws IOException, HttpException {
ArrayList headers = new ArrayList();
String name = null;
StringBuffer value = null;
for (; ; ) {
String line = HttpParser.readLine(is, charset);
if ((line == null) || (line.trim().length() < 1)) {
break;
}
// discussion on folded headers
if ((line.charAt(0) == ' ') || (line.charAt(0) == '\t')) {
// so append value
if (value != null) {
value.append(' ');
value.append(line.trim());
}
} else {
// make sure we save the previous name,value pair if present
if (name != null) {
headers.add(new Header(name, value.toString()));
}
// Otherwise we should have normal HTTP header line
// Parse the header name and value
int colon = line.indexOf(":");
if (colon < 0) {
// Do not thrown the exception ignore it instead
// throw new ProtocolException("Unable to parse header: " + line);
logger.warn("Ignoring malformed HTTP header line: \"" + line + "\"");
name = null;
value = null;
} else {
name = line.substring(0, colon).trim();
value = new StringBuffer(line.substring(colon + 1).trim());
}
}
}
// make sure we save the last name,value pair if present
if (name != null) {
headers.add(new Header(name, value.toString()));
}
return (Header[]) headers.toArray(new Header[headers.size()]);
}
use of org.apache.commons.httpclient.Header in project hadoop by apache.
the class SwiftRestClient method execWithDebugOutput.
/**
* Execute the request with the request and response logged at debug level
* @param method method to execute
* @param client client to use
* @param <M> method type
* @return the status code
* @throws IOException any failure reported by the HTTP client.
*/
private <M extends HttpMethod> int execWithDebugOutput(M method, HttpClient client) throws IOException {
if (LOG.isDebugEnabled()) {
StringBuilder builder = new StringBuilder(method.getName() + " " + method.getURI() + "\n");
for (Header header : method.getRequestHeaders()) {
builder.append(header.toString());
}
LOG.debug(builder);
}
int statusCode = client.executeMethod(method);
if (LOG.isDebugEnabled()) {
LOG.debug("Status code = " + statusCode);
}
return statusCode;
}
use of org.apache.commons.httpclient.Header in project hadoop by apache.
the class TestSwiftRestClient method testPutAndDelete.
@Test(timeout = SWIFT_TEST_TIMEOUT)
public void testPutAndDelete() throws Throwable {
assumeEnabled();
SwiftRestClient client = createClient();
client.authenticate();
Path path = new Path("restTestPutAndDelete");
SwiftObjectPath sobject = SwiftObjectPath.fromPath(serviceURI, path);
byte[] stuff = new byte[1];
stuff[0] = 'a';
client.upload(sobject, new ByteArrayInputStream(stuff), stuff.length);
//check file exists
Duration head = new Duration();
Header[] responseHeaders = client.headRequest("expect success", sobject, SwiftRestClient.NEWEST);
head.finished();
LOG.info("head request duration " + head);
for (Header header : responseHeaders) {
LOG.info(header.toString());
}
//delete the file
client.delete(sobject);
//check file is gone
try {
Header[] headers = client.headRequest("expect fail", sobject, SwiftRestClient.NEWEST);
Assert.fail("Expected deleted file, but object is still present: " + sobject);
} catch (FileNotFoundException e) {
//expected
}
for (DurationStats stats : client.getOperationStatistics()) {
LOG.info(stats);
}
}
use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.
the class WebDavClient method logRequestInfo.
private void logRequestInfo(HttpMethod method, String body) throws IOException {
if (!mDebugEnabled) {
return;
}
StringBuilder reqLog = new StringBuilder();
reqLog.append("WebDAV request:\n").append(method.getName()).append(" ").append(method.getURI().toString());
reqLog.append('\n');
Header[] headers = method.getRequestHeaders();
if (headers != null && headers.length > 0) {
for (Header hdr : headers) {
String hdrName = hdr.getName();
reqLog.append(hdrName).append('=');
if (hdrName.contains("Auth") || (hdrName.contains(HttpHeaders.COOKIE))) {
reqLog.append("*** REPLACED ***\n");
} else {
reqLog.append(hdr.getValue()).append('\n');
}
}
}
if (Strings.isNullOrEmpty(body) || !ZimbraLog.dav.isTraceEnabled()) {
ZimbraLog.dav.debug(reqLog.toString());
} else {
ZimbraLog.dav.debug("%s\n%s", reqLog.toString(), body);
}
}
use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.
the class WebDavClient method executeFollowRedirect.
protected HttpMethod executeFollowRedirect(DavRequest req) throws IOException {
HttpMethod method = null;
boolean done = false;
while (!done) {
method = execute(req);
int ret = method.getStatusCode();
if (ret == HttpStatus.SC_MOVED_PERMANENTLY || ret == HttpStatus.SC_MOVED_TEMPORARILY) {
Header newLocation = method.getResponseHeader("Location");
if (newLocation != null) {
String uri = newLocation.getValue();
ZimbraLog.dav.debug("redirect to new url = " + uri);
method.releaseConnection();
req.setRedirectUrl(uri);
continue;
}
}
done = true;
}
return method;
}
Aggregations