1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common/log_wrapper.h"
17 #include "http.h"
18
19 namespace OHOS::ArkCompiler::Toolchain {
20 /* static */
DecodeHeader(const std::string & headersText,std::string_view headerName)21 std::string HttpBase::DecodeHeader(const std::string& headersText, std::string_view headerName)
22 {
23 auto startPos = headersText.find(headerName);
24 if (startPos != std::string::npos) {
25 auto endOfLinePos = headersText.find(EOL, startPos);
26 startPos += headerName.size();
27 if (startPos < headersText.size() && startPos < endOfLinePos) {
28 return headersText.substr(startPos, endOfLinePos - startPos);
29 }
30 }
31 return "";
32 }
33
34 /* static */
DecodeVersion(const std::string & request,std::string::size_type methodStartPos)35 std::string HttpRequest::DecodeVersion(const std::string& request, std::string::size_type methodStartPos)
36 {
37 if (methodStartPos >= request.size()) {
38 return "";
39 }
40
41 auto endOfLinePos = request.find(EOL, methodStartPos);
42 // the typical header is "GET /chat HTTP/1.1", where protocol version is located after the second space symbol
43 methodStartPos = request.find(' ', methodStartPos);
44 if (methodStartPos != std::string::npos) {
45 methodStartPos = request.find(' ', methodStartPos + 1);
46 }
47 if (methodStartPos != std::string::npos &&
48 methodStartPos + 1 < request.size() &&
49 methodStartPos + 1 < endOfLinePos) {
50 return request.substr(methodStartPos + 1, endOfLinePos - (methodStartPos + 1));
51 }
52 return "";
53 }
54
55 // request example can be found at https://www.rfc-editor.org/rfc/rfc6455#section-1.3
56 /* static */
Decode(const std::string & request,HttpRequest & parsed)57 bool HttpRequest::Decode(const std::string& request, HttpRequest& parsed)
58 {
59 auto pos = request.find(GET);
60 if (pos == std::string::npos) {
61 LOGW("Handshake failed: lack of necessary info");
62 return false;
63 }
64
65 if (request.find(ORIGIN) != std::string::npos) {
66 return false;
67 }
68
69 parsed.version = DecodeVersion(request, pos);
70 parsed.connection = DecodeHeader(request, CONNECTION);
71 parsed.upgrade = DecodeHeader(request, UPGRADE);
72 parsed.secWebSocketKey = DecodeHeader(request, SEC_WEBSOCKET_KEY);
73
74 return true;
75 }
76
77 /* static */
DecodeVersion(const std::string & response,std::string::size_type versionStartPos)78 std::string HttpResponse::DecodeVersion(const std::string& response, std::string::size_type versionStartPos)
79 {
80 // status-line example: "HTTP/1.1 404 Not Found"
81 if (versionStartPos < response.size()) {
82 auto versionEndPos = response.find(' ', versionStartPos);
83 if (versionEndPos != std::string::npos) {
84 return response.substr(versionStartPos, versionEndPos - versionStartPos);
85 }
86 }
87 return "";
88 }
89
90 /* static */
DecodeStatus(const std::string & response,std::string::size_type versionEndPos)91 std::string HttpResponse::DecodeStatus(const std::string& response, std::string::size_type versionEndPos)
92 {
93 // status-line example: "HTTP/1.1 404 Not Found"
94 if (versionEndPos < response.size() && response[versionEndPos] == ' ') {
95 auto statusStartPos = response.find_first_not_of(' ', versionEndPos);
96 if (statusStartPos != std::string::npos) {
97 auto statusEndPos = response.find(' ', statusStartPos);
98 statusEndPos = (statusEndPos == std::string::npos) ? response.find(EOL, statusStartPos) : statusEndPos;
99 if (statusEndPos != std::string::npos) {
100 return response.substr(statusStartPos, statusEndPos - statusStartPos);
101 }
102 }
103 }
104 return "";
105 }
106
107 // request example can be found at https://www.rfc-editor.org/rfc/rfc6455#section-1.2
108 /* static */
Decode(const std::string & response,HttpResponse & parsed)109 bool HttpResponse::Decode(const std::string& response, HttpResponse& parsed)
110 {
111 // find start of status-line
112 auto versionStartPos = response.find("HTTP");
113 if (versionStartPos == std::string::npos) {
114 LOGW("Handshake failed: lack of necessary info, no status-line found");
115 return false;
116 }
117
118 parsed.version = DecodeVersion(response, versionStartPos);
119 parsed.status = DecodeStatus(response, versionStartPos + parsed.version.size());
120 parsed.connection = DecodeHeader(response, CONNECTION);
121 parsed.upgrade = DecodeHeader(response, UPGRADE);
122 parsed.secWebSocketAccept = DecodeHeader(response, SEC_WEBSOCKET_ACCEPT);
123
124 return true;
125 }
126 } // namespace OHOS::ArkCompiler::Toolchain
127