xref: /trafficserver/src/tscpp/api/Response.cc (revision 4cfd5a73)
1 /**
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10       http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17  */
18 /**
19  * @file Response.cc
20  */
21 #include "tscpp/api/Response.h"
22 #include "tscpp/api/noncopyable.h"
23 #include "utils_internal.h"
24 #include "logging_internal.h"
25 
26 using namespace atscppapi;
27 using std::string;
28 
29 namespace atscppapi
30 {
31 /**
32  * @private
33  */
34 struct ResponseState : noncopyable {
35   TSMBuffer hdr_buf_ = nullptr;
36   TSMLoc hdr_loc_    = nullptr;
37   Headers headers_;
38   ResponseState() = default;
39 };
40 } // namespace atscppapi
41 
Response()42 Response::Response()
43 {
44   state_ = new ResponseState();
45   //  state_->headers_.setType(Headers::TYPE_RESPONSE);
46 }
47 
48 void
init(void * hdr_buf,void * hdr_loc)49 Response::init(void *hdr_buf, void *hdr_loc)
50 {
51   reset();
52   if (!hdr_buf || !hdr_loc) {
53     return;
54   }
55   state_->hdr_buf_ = static_cast<TSMBuffer>(hdr_buf);
56   state_->hdr_loc_ = static_cast<TSMLoc>(hdr_loc);
57   state_->headers_.reset(state_->hdr_buf_, state_->hdr_loc_);
58   LOG_DEBUG("Initializing response %p with hdr_buf=%p and hdr_loc=%p", this, state_->hdr_buf_, state_->hdr_loc_);
59 }
60 
61 void
reset()62 Response::reset()
63 {
64   state_->hdr_buf_ = nullptr;
65   state_->hdr_loc_ = nullptr;
66   state_->headers_.reset(nullptr, nullptr);
67   LOG_DEBUG("Reset response %p", this);
68 }
69 
70 HttpVersion
getVersion() const71 Response::getVersion() const
72 {
73   HttpVersion ret_val = HTTP_VERSION_UNKNOWN;
74   if (state_->hdr_buf_ && state_->hdr_loc_) {
75     ret_val = utils::internal::getHttpVersion(state_->hdr_buf_, state_->hdr_loc_);
76     LOG_DEBUG("Initializing response version to %d [%s] with hdr_buf=%p and hdr_loc=%p", ret_val,
77               HTTP_VERSION_STRINGS[ret_val].c_str(), state_->hdr_buf_, state_->hdr_loc_);
78   }
79   return ret_val;
80 }
81 
82 HttpStatus
getStatusCode() const83 Response::getStatusCode() const
84 {
85   HttpStatus ret_val = HTTP_STATUS_UNKNOWN;
86   if (state_->hdr_buf_ && state_->hdr_loc_) {
87     ret_val = static_cast<HttpStatus>(TSHttpHdrStatusGet(state_->hdr_buf_, state_->hdr_loc_));
88     LOG_DEBUG("Initializing response status code to %d with hdr_buf=%p and hdr_loc=%p", ret_val, state_->hdr_buf_,
89               state_->hdr_loc_);
90   }
91   return ret_val;
92 }
93 
94 void
setStatusCode(HttpStatus code)95 Response::setStatusCode(HttpStatus code)
96 {
97   if (state_->hdr_buf_ && state_->hdr_loc_) {
98     TSHttpHdrStatusSet(state_->hdr_buf_, state_->hdr_loc_, static_cast<TSHttpStatus>(code));
99     LOG_DEBUG("Changing response status code to %d with hdr_buf=%p and hdr_loc=%p", code, state_->hdr_buf_, state_->hdr_loc_);
100   }
101 }
102 
103 string
getReasonPhrase() const104 Response::getReasonPhrase() const
105 {
106   string ret_str;
107   if (state_->hdr_buf_ && state_->hdr_loc_) {
108     int length;
109     const char *str = TSHttpHdrReasonGet(state_->hdr_buf_, state_->hdr_loc_, &length);
110     if (str && length) {
111       ret_str.assign(str, length);
112       LOG_DEBUG("Initializing response reason phrase to '%s' with hdr_buf=%p and hdr_loc=%p", ret_str.c_str(), state_->hdr_buf_,
113                 state_->hdr_loc_);
114     } else {
115       LOG_ERROR("TSHttpHdrReasonGet returned null string or zero length. str=%p, length=%d, hdr_buf=%p, hdr_loc=%p", str, length,
116                 state_->hdr_buf_, state_->hdr_loc_);
117     }
118   }
119   return ret_str; // if not initialized, we will just return an empty string
120 }
121 
122 void
setReasonPhrase(const string & phrase)123 Response::setReasonPhrase(const string &phrase)
124 {
125   if (state_->hdr_buf_ && state_->hdr_loc_) {
126     TSHttpHdrReasonSet(state_->hdr_buf_, state_->hdr_loc_, phrase.c_str(), phrase.length());
127     LOG_DEBUG("Changing response reason phrase to '%s' with hdr_buf=%p and hdr_loc=%p", phrase.c_str(), state_->hdr_buf_,
128               state_->hdr_loc_);
129   }
130 }
131 
132 Headers &
getHeaders() const133 Response::getHeaders() const
134 {
135   return state_->headers_; // if not initialized, we will just return an empty object
136 }
137 
~Response()138 Response::~Response()
139 {
140   delete state_;
141 }
142