Two HTTP concepts that developers frequently misunderstand: read timeout is almost never what you think it is, and OAuth 2.0 is a protocol with a very specific two-step structure that exists for a concrete security reason.
Read Timeout — Per Chunk, Not Per Response
The most common misconception about read timeout:
“Read timeout is the maximum time allowed for the full response to arrive.”
This is wrong. Read timeout does not measure the total time from sending a request to receiving the last byte of the response.
How Data Is Actually Read
HTTP responses are not delivered as one atomic transfer. Data arrives in chunks — small units read one at a time. Consider a server sending a 1024-byte file, with the client reading 2 bytes per iteration:
Client → Server: "give me the next 2 bytes"
Server → Client: bytes 0–1
Client → Server: "give me the next 2 bytes"
Server → Client: bytes 2–3
... (512 round trips for 1024 bytes)
The read timeout timer starts each time the client reaches out for the next chunk. The server must deliver that chunk within the timeout window. If it does, the timer resets for the next chunk. If it does not, the connection is broken.
A read timeout of 10 seconds means: “the server must deliver each individual chunk within 10 seconds.” A large file can take minutes to download with a 10-second read timeout — as long as each chunk arrives within its own 10-second window.
This is why a short read timeout does not prevent large downloads. It prevents stalled connections — situations where the server stops responding mid-transfer without closing the connection.
Write Timeout — The Same Concept for Uploads
Write timeout is the mirror of read timeout, applied to the upload direction.
When uploading a file, the client sends data in chunks. The server expects each chunk to arrive within the write timeout window. If the client’s upload stalls — network congestion, a slow device, a paused upload — and a chunk does not arrive within the window, the server closes the connection.
Client → [chunk 1] → Server server timer starts
Client → [chunk 2] → Server server timer resets
Client stalls for 30+ seconds
Server: write timeout exceeded → connection closed
OkHttp Configuration
val client = OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS) // DNS + TLS handshake
.readTimeout(30, TimeUnit.SECONDS) // per-chunk read window
.writeTimeout(30, TimeUnit.SECONDS) // per-chunk write window
.build()
connectTimeout covers the initial connection establishment (step 1 in every request — DNS resolution and TLS handshake). It measures the total time for that one-time setup, not per-chunk. readTimeout and writeTimeout apply per chunk during data transfer.
Setting read or write timeout to 0 disables it entirely — useful for streaming responses where chunks may be deliberately spaced far apart.
OAuth 2.0 — Authentication Then Authorization
OAuth 2.0 is a protocol with two distinct steps. Understanding why both steps exist requires first seeing the problem it solves.
The Problem Without OAuth
Without a token-based system, every request to a protected server must carry the user’s credentials:
GET /profile Authorization: email=user@example.com&password=secret
GET /orders Authorization: email=user@example.com&password=secret
POST /checkout Authorization: email=user@example.com&password=secret
The password travels over the network on every request. It appears in server access logs. Any intercepted request exposes the full credential. If one request is compromised, the attacker has the password — permanently, until the user changes it.
Step 1 — Authentication: Who Are You?
The user provides credentials once to an authentication endpoint:
POST /auth
Body: { "email": "user@example.com", "password": "secret" }
The server verifies the identity and responds with two tokens:
- Access token — short-lived (minutes to hours). Used for API calls.
- Refresh token — long-lived (days to weeks). Used only to obtain a new access token when the current one expires.
The password is no longer needed after this step.
Step 2 — Authorization: What Are You Allowed to Do?
All subsequent API calls carry the access token in the Authorization header:
GET /profile Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
GET /orders Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
The server validates the token — checking its signature and expiry — without ever seeing the password again. The token encodes the user’s identity and permissions. If an access token is intercepted, it expires quickly. The password itself is never exposed after step 1.
Token Refresh Flow
When the access token expires:
- The app detects a
401 Unauthorizedresponse. - It sends the refresh token to the token refresh endpoint.
- The server issues a new access token (and optionally a new refresh token).
- The app retries the original request with the new access token.
If the refresh token also expires (or is revoked), the user must go through step 1 again — re-entering their credentials.
Implementing Token Refresh with OkHttp Interceptor
class AuthInterceptor(private val tokenStore: TokenStore) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer ${tokenStore.accessToken}")
.build()
val response = chain.proceed(request)
if (response.code == 401) {
// Access token expired — refresh it
val newToken = tokenStore.refresh()
val retryRequest = request.newBuilder()
.header("Authorization", "Bearer $newToken")
.build()
return chain.proceed(retryRequest)
}
return response
}
}
The interceptor adds the token to every request automatically and handles the refresh silently when a 401 is received. The rest of the app never manages tokens directly.
Summary
| Concept | Key point |
|---|---|
| Read timeout | Timer resets per chunk — not total response time |
| Write timeout | Same per-chunk logic, applied to upload direction |
connectTimeout | Total time for DNS + TLS handshake (one-time per connection) |
| OAuth 2.0 Step 1 | Authentication — verify identity, receive tokens |
| OAuth 2.0 Step 2 | Authorization — use access token for every API call |
| Access token | Short-lived — exposed in headers but expires quickly |
| Refresh token | Long-lived — used only to renew access tokens |
| No OAuth | Password sent on every request — permanent exposure risk |