| 298 | } |
| 299 | |
| 300 | static TSReturnCode |
| 301 | WriteResponseHeader(StaticHitRequest *trq, TSHttpStatus status) |
| 302 | { |
| 303 | StaticHitHttpHeader response; |
| 304 | |
| 305 | VDEBUG("writing response header"); |
| 306 | |
| 307 | if (TSHttpHdrTypeSet(response.buffer, response.header, TS_HTTP_TYPE_RESPONSE) != TS_SUCCESS) { |
| 308 | VERROR("failed to set type"); |
| 309 | return TS_ERROR; |
| 310 | } |
| 311 | if (TSHttpHdrVersionSet(response.buffer, response.header, TS_HTTP_VERSION(1, 1)) != TS_SUCCESS) { |
| 312 | VERROR("failed to set HTTP version"); |
| 313 | return TS_ERROR; |
| 314 | } |
| 315 | if (TSHttpHdrStatusSet(response.buffer, response.header, status) != TS_SUCCESS) { |
| 316 | VERROR("failed to set HTTP status"); |
| 317 | return TS_ERROR; |
| 318 | } |
| 319 | |
| 320 | TSHttpHdrReasonSet(response.buffer, response.header, TSHttpHdrReasonLookup(status), -1); |
| 321 | |
| 322 | if (status == TS_HTTP_STATUS_OK) { |
| 323 | // Set the Content-Length header. |
| 324 | HeaderFieldIntSet(response, TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH, trq->nbytes); |
| 325 | |
| 326 | // Set the Cache-Control header. |
| 327 | if (trq->maxAge > 0) { |
| 328 | char buf[64]; |
| 329 | |
| 330 | snprintf(buf, sizeof(buf), "max-age=%u", trq->maxAge); |
| 331 | HeaderFieldStringSet(response, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL, buf); |
| 332 | HeaderFieldDateSet(response, TS_MIME_FIELD_LAST_MODIFIED, TS_MIME_LEN_LAST_MODIFIED, time(nullptr)); |
| 333 | |
| 334 | } else { |
| 335 | HeaderFieldStringSet(response, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL, "no-cache"); |
| 336 | } |
| 337 | |
| 338 | HeaderFieldStringSet(response, TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE, trq->mimeType.c_str()); |
| 339 | } |
| 340 | |
| 341 | // Write the header to the IO buffer. Set the VIO bytes so that we can get a WRITE_COMPLETE |
| 342 | // event when this is done. |
| 343 | int hdrlen = TSHttpHdrLengthGet(response.buffer, response.header); |
| 344 | |
| 345 | TSHttpHdrPrint(response.buffer, response.header, trq->writeio.iobuf); |
| 346 | TSVIONBytesSet(trq->writeio.vio, hdrlen); |
| 347 | TSVIOReenable(trq->writeio.vio); |
| 348 | |
| 349 | TSStatIntIncrement(StatCountBytes, hdrlen); |
| 350 | |
| 351 | return TS_SUCCESS; |
| 352 | } |
| 353 | |
| 354 | static bool |
| 355 | StaticHitParseRequest(StaticHitRequest *trq) |
no test coverage detected