diff options
| author | molotkov-and <[email protected]> | 2023-03-06 11:56:06 +0300 |
|---|---|---|
| committer | molotkov-and <[email protected]> | 2023-03-06 11:56:06 +0300 |
| commit | 57795f2a47a7dd920380f7df0a02f9033f5d6103 (patch) | |
| tree | b0ded297e6df4a1b087c9581afe54b5c20cc7d79 /library/cpp/actors/http/http.cpp | |
| parent | 477afee9815c0675df743b46d486d751b88f9885 (diff) | |
Add capability to set headers to outgoing response
Diffstat (limited to 'library/cpp/actors/http/http.cpp')
| -rw-r--r-- | library/cpp/actors/http/http.cpp | 53 |
1 files changed, 46 insertions, 7 deletions
diff --git a/library/cpp/actors/http/http.cpp b/library/cpp/actors/http/http.cpp index f5d7f5cf5c5..a8e2c099221 100644 --- a/library/cpp/actors/http/http.cpp +++ b/library/cpp/actors/http/http.cpp @@ -452,7 +452,7 @@ THttpIncomingResponse::THttpIncomingResponse(THttpOutgoingRequestPtr request) : Request(request) {} -THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, TStringBuf message, TStringBuf contentType, TStringBuf body, TInstant lastModified) { +THttpOutgoingResponsePtr THttpIncomingRequest::CreateIncompleteResponse(TStringBuf status, TStringBuf message) { TStringBuf version = Version; if (version != "1.0" && version != "1.1") { version = "1.1"; @@ -462,19 +462,30 @@ THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, if (!Endpoint->WorkerName.empty()) { response->Set("X-Worker-Name", Endpoint->WorkerName); } - if (!contentType.empty() && !body.empty()) { - response->Set<&THttpResponse::ContentType>(contentType); + return response; +} + +THttpOutgoingResponsePtr THttpIncomingRequest::CreateIncompleteResponse(TStringBuf status, TStringBuf message, const THeaders& headers) { + THttpOutgoingResponsePtr response = CreateIncompleteResponse(status, message); + response->Set(headers); + return response; +} + +THttpOutgoingResponsePtr THttpIncomingRequest::CreateIncompleteResponse(TStringBuf status, TStringBuf message, const THeaders& headers, TStringBuf body) { + THttpOutgoingResponsePtr response = CreateIncompleteResponse(status, message, headers); + if (!response->ContentType.empty() && !body.empty()) { if (!Endpoint->CompressContentTypes.empty()) { - contentType = contentType.Before(';'); + TStringBuf contentType = response->ContentType.Before(';'); Trim(contentType, ' '); if (Count(Endpoint->CompressContentTypes, contentType) != 0) { response->EnableCompression(); } } } - if (lastModified) { - response->Set<&THttpResponse::LastModified>(lastModified.FormatGmTime("%a, %d %b %Y %H:%M:%S GMT")); - } + return response; +} + +void THttpIncomingRequest::FinishResponse(THttpOutgoingResponsePtr& response, TStringBuf body) { if (response->IsNeedBody() || !body.empty()) { if (Method == "HEAD") { response->Set<&THttpResponse::ContentLength>(ToString(body.size())); @@ -482,9 +493,37 @@ THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, response->SetBody(body); } } +} + +THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, TStringBuf message) { + THttpOutgoingResponsePtr response = CreateIncompleteResponse(status, message); + FinishResponse(response); + return response; +} + +THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, TStringBuf message, const THeaders& headers) { + THttpOutgoingResponsePtr response = CreateIncompleteResponse(status, message, headers); + FinishResponse(response); + return response; +} + +THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, TStringBuf message, const THeaders& headers, TStringBuf body) { + THttpOutgoingResponsePtr response = CreateIncompleteResponse(status, message, headers, body); + FinishResponse(response, body); return response; } +THttpOutgoingResponsePtr THttpIncomingRequest::CreateResponse(TStringBuf status, TStringBuf message, TStringBuf contentType, TStringBuf body, TInstant lastModified) { + NHttp::THeadersBuilder headers; + if (!contentType.empty() && !body.empty()) { + headers.Set("Content-Type", contentType); + } + if (lastModified) { + headers.Set("Last-Modified", lastModified.FormatGmTime("%a, %d %b %Y %H:%M:%S GMT")); + } + return CreateResponse(status, message, headers, body); +} + THttpIncomingRequestPtr THttpIncomingRequest::Duplicate() { THttpIncomingRequestPtr request = new THttpIncomingRequest(*this); request->Reparse(); |
