Search in sources :

Example 71 with Headers

use of com.amazonaws.services.s3.Headers in project edx-app-android by edx.

the class OnlyIfCachedStrippingInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    if (request.cacheControl().onlyIfCached()) {
        final Headers headers = request.headers();
        final Headers.Builder strippedHeadersBuilder = new Headers.Builder();
        for (int i = 0, headersCount = headers.size(); i < headersCount; i++) {
            final String headerName = headers.name(i);
            String headerValue = headers.value(i);
            if (headerName.equalsIgnoreCase("Cache-Control")) {
                Matcher directiveMatcher = PATTERN_ONLY_IF_CACHED_HEADER.matcher(headerValue);
                if (directiveMatcher.find()) {
                    while (true) {
                        final StringBuffer newHeaderValueBuffer = new StringBuffer();
                        directiveMatcher.appendReplacement(newHeaderValueBuffer, "$" + (directiveMatcher.group(GROUP_SEPARATOR_START).isEmpty() ? GROUP_SEPARATOR_END : GROUP_SEPARATOR_START));
                        directiveMatcher.appendTail(newHeaderValueBuffer);
                        headerValue = newHeaderValueBuffer.toString();
                        directiveMatcher = PATTERN_ONLY_IF_CACHED_HEADER.matcher(headerValue);
                        if (!directiveMatcher.find())
                            break;
                    }
                    if (headerName.isEmpty())
                        continue;
                }
            }
            strippedHeadersBuilder.add(headerName, headerValue);
        }
        request = request.newBuilder().headers(strippedHeadersBuilder.build()).build();
    }
    return chain.proceed(request);
}
Also used : Matcher(java.util.regex.Matcher) Headers(okhttp3.Headers) Request(okhttp3.Request)

Example 72 with Headers

use of com.amazonaws.services.s3.Headers in project okhttp-OkGo by jeasonlzy.

the class HttpLoggingInterceptor method logForRequest.

private void logForRequest(Request request, Connection connection) throws IOException {
    boolean logBody = (printLevel == Level.BODY);
    boolean logHeaders = (printLevel == Level.BODY || printLevel == Level.HEADERS);
    RequestBody requestBody = request.body();
    boolean hasRequestBody = requestBody != null;
    Protocol protocol = connection != null ? connection.protocol() : Protocol.HTTP_1_1;
    try {
        String requestStartMessage = "--> " + request.method() + ' ' + request.url() + ' ' + protocol;
        log(requestStartMessage);
        if (logHeaders) {
            Headers headers = request.headers();
            for (int i = 0, count = headers.size(); i < count; i++) {
                log("\t" + headers.name(i) + ": " + headers.value(i));
            }
            log(" ");
            if (logBody && hasRequestBody) {
                if (isPlaintext(requestBody.contentType())) {
                    bodyToString(request);
                } else {
                    log("\tbody: maybe [file part] , too large too print , ignored!");
                }
            }
        }
    } catch (Exception e) {
        OkLogger.e(e);
    } finally {
        log("--> END " + request.method());
    }
}
Also used : HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) Protocol(okhttp3.Protocol) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 73 with Headers

use of com.amazonaws.services.s3.Headers in project okhttputils by hongyangAndroid.

the class LoggerInterceptor method logForRequest.

private void logForRequest(Request request) {
    try {
        String url = request.url().toString();
        Headers headers = request.headers();
        Log.e(tag, "========request'log=======");
        Log.e(tag, "method : " + request.method());
        Log.e(tag, "url : " + url);
        if (headers != null && headers.size() > 0) {
            Log.e(tag, "headers : " + headers.toString());
        }
        RequestBody requestBody = request.body();
        if (requestBody != null) {
            MediaType mediaType = requestBody.contentType();
            if (mediaType != null) {
                Log.e(tag, "requestBody's contentType : " + mediaType.toString());
                if (isText(mediaType)) {
                    Log.e(tag, "requestBody's content : " + bodyToString(request));
                } else {
                    Log.e(tag, "requestBody's content : " + " maybe [file part] , too large too print , ignored!");
                }
            }
        }
        Log.e(tag, "========request'log=======end");
    } catch (Exception e) {
    //            e.printStackTrace();
    }
}
Also used : Headers(okhttp3.Headers) MediaType(okhttp3.MediaType) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 74 with Headers

use of com.amazonaws.services.s3.Headers in project okhttp by square.

the class JavaApiConverterTest method createOkRequest_nullRequestHeaderKey.

// Older versions of OkHttp would store the "request line" as a header with a
// null key. To support the Android usecase where an old version of OkHttp uses
// a newer, Android-bundled, version of HttpResponseCache the null key must be
// explicitly ignored.
@Test
public void createOkRequest_nullRequestHeaderKey() throws Exception {
    URI uri = new URI("https://foo/bar");
    Map<String, List<String>> javaRequestHeaders = new LinkedHashMap<>();
    javaRequestHeaders.put(null, Arrays.asList("GET / HTTP 1.1"));
    javaRequestHeaders.put("Foo", Arrays.asList("Bar"));
    Request request = JavaApiConverter.createOkRequest(uri, "POST", javaRequestHeaders);
    assertTrue(request.isHttps());
    assertEquals(uri, request.url().uri());
    Headers okRequestHeaders = request.headers();
    assertEquals(1, okRequestHeaders.size());
    assertEquals("Bar", okRequestHeaders.get("Foo"));
    assertEquals("POST", request.method());
}
Also used : Headers(okhttp3.Headers) Request(okhttp3.Request) List(java.util.List) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 75 with Headers

use of com.amazonaws.services.s3.Headers in project okhttp by square.

the class Main method run.

@Override
public void run() {
    if (showHelpIfRequested()) {
        return;
    }
    if (version) {
        System.out.println(NAME + " " + versionString());
        System.out.println("Protocols: " + protocols());
        return;
    }
    if (showHttp2Frames) {
        enableHttp2FrameLogging();
    }
    client = createClient();
    Request request = createRequest();
    try {
        Response response = client.newCall(request).execute();
        if (showHeaders) {
            System.out.println(StatusLine.get(response));
            Headers headers = response.headers();
            for (int i = 0, size = headers.size(); i < size; i++) {
                System.out.println(headers.name(i) + ": " + headers.value(i));
            }
            System.out.println();
        }
        // Stream the response to the System.out as it is returned from the server.
        Sink out = Okio.sink(System.out);
        BufferedSource source = response.body().source();
        while (!source.exhausted()) {
            out.write(source.buffer(), source.buffer().size());
            out.flush();
        }
        response.body().close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close();
    }
}
Also used : Response(okhttp3.Response) Sink(okio.Sink) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) BufferedSource(okio.BufferedSource)

Aggregations

Headers (okhttp3.Headers)128 Request (okhttp3.Request)61 Response (okhttp3.Response)54 Test (org.junit.Test)40 IOException (java.io.IOException)32 Call (okhttp3.Call)30 RequestBody (okhttp3.RequestBody)25 CancelledException (com.hippo.ehviewer.client.exception.CancelledException)20 EhException (com.hippo.ehviewer.client.exception.EhException)20 NoHAtHClientException (com.hippo.ehviewer.client.exception.NoHAtHClientException)20 ParseException (com.hippo.ehviewer.client.exception.ParseException)20 StatusCodeException (com.hippo.network.StatusCodeException)20 ResponseBody (okhttp3.ResponseBody)18 HttpHeaders (okhttp3.internal.http.HttpHeaders)18 MediaType (okhttp3.MediaType)15 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)14 HeaderResponseBoolHeaders (fixtures.header.models.HeaderResponseBoolHeaders)14 HeaderResponseByteHeaders (fixtures.header.models.HeaderResponseByteHeaders)14 HeaderResponseDateHeaders (fixtures.header.models.HeaderResponseDateHeaders)14 HeaderResponseDatetimeHeaders (fixtures.header.models.HeaderResponseDatetimeHeaders)14