1 /* 2 * Copyright 2022 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef FCP_CLIENT_HTTP_CURL_CURL_HEADER_PARSER_H_ 17 #define FCP_CLIENT_HTTP_CURL_CURL_HEADER_PARSER_H_ 18 19 #include <string> 20 21 #include "fcp/client/http/http_client.h" 22 23 namespace fcp::client::http::curl { 24 25 // A custom parser that is needed to call the first callback after all the 26 // headers received 27 class CurlHeaderParser { 28 public: 29 explicit CurlHeaderParser(); 30 ~CurlHeaderParser() = default; 31 CurlHeaderParser(const CurlHeaderParser&) = delete; 32 CurlHeaderParser& operator=(const CurlHeaderParser&) = delete; 33 34 // Parses the next header string 35 void ParseHeader(const std::string& header_string); 36 37 // Removes the "Content-Encoding", "Content-Length", and "Content-Length" 38 // headers from the response when the curl encoding in use because they 39 // reflect in-flight encoded values 40 void UseCurlEncoding(); 41 // Indicates that the parser reached the last header 42 ABSL_MUST_USE_RESULT bool IsLastHeader() const; 43 ABSL_MUST_USE_RESULT int GetStatusCode() const; 44 ABSL_MUST_USE_RESULT HeaderList GetHeaderList() const; 45 46 private: 47 // Extracts status codes from HTTP/1.1 and HTTP/2 responses 48 bool ParseAsStatus(const std::string& header_string); 49 // Parses a header into a key-value pair 50 bool ParseAsHeader(const std::string& header_string); 51 // Decides whether it is the last header 52 bool ParseAsLastLine(const std::string& header_string); 53 54 int status_code_; 55 HeaderList header_list_; 56 bool is_last_header_line_; 57 bool use_curl_encoding_; 58 }; 59 60 } // namespace fcp::client::http::curl 61 62 #endif // FCP_CLIENT_HTTP_CURL_CURL_HEADER_PARSER_H_ 63