| 135 | } |
| 136 | |
| 137 | Status ServerSessionMiddlewareFactory::StartCall( |
| 138 | const CallInfo&, const ServerCallContext& context, |
| 139 | std::shared_ptr<ServerMiddleware>* middleware) { |
| 140 | std::string session_id; |
| 141 | |
| 142 | const std::pair<CallHeaders::const_iterator, CallHeaders::const_iterator>& |
| 143 | headers_it_pr = context.incoming_headers().equal_range("cookie"); |
| 144 | for (auto itr = headers_it_pr.first; itr != headers_it_pr.second; ++itr) { |
| 145 | const std::string_view& cookie_header = itr->second; |
| 146 | const std::vector<std::pair<std::string, std::string>> cookies = |
| 147 | ParseCookieString(cookie_header); |
| 148 | for (const std::pair<std::string, std::string>& cookie : cookies) { |
| 149 | if (cookie.first == kSessionCookieName) { |
| 150 | if (cookie.second.empty()) |
| 151 | return Status::Invalid("Empty ", kSessionCookieName, " cookie value."); |
| 152 | session_id = std::move(cookie.second); |
| 153 | } |
| 154 | } |
| 155 | if (!session_id.empty()) break; |
| 156 | } |
| 157 | |
| 158 | if (session_id.empty()) { |
| 159 | // No cookie was found |
| 160 | // Temporary workaround until middleware handling fixed |
| 161 | auto [id, s] = CreateNewSession(); |
| 162 | *middleware = std::make_shared<ServerSessionMiddlewareImpl>( |
| 163 | this, context.incoming_headers(), std::move(s), id, false); |
| 164 | } else { |
| 165 | const std::shared_lock<std::shared_mutex> l(session_store_lock_); |
| 166 | if (auto it = session_store_.find(session_id); it == session_store_.end()) { |
| 167 | return Status::Invalid("Invalid or expired ", kSessionCookieName, " cookie."); |
| 168 | } else { |
| 169 | auto session = it->second; |
| 170 | *middleware = std::make_shared<ServerSessionMiddlewareImpl>( |
| 171 | this, context.incoming_headers(), std::move(session), session_id); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return Status::OK(); |
| 176 | } |
| 177 | |
| 178 | /// \brief Get a new, empty session option map & its id key; {"",NULLPTR} on collision. |
| 179 | std::pair<std::string, std::shared_ptr<FlightSession>> |
no test coverage detected