Size: 7906 bytes.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// cs/net/http/client.cc
#include "cs/net/http/client.hh"

#include <curl/curl.h>

#include <map>
#include <sstream>
#include <string>

namespace cs {
namespace {
static size_t CurlWriteCallback(void* contents, size_t size,
                                size_t nmemb, void* userp) {
  reinterpret_cast<std::string*>(userp)->append(
      (char*)contents, size * nmemb);
  return size * nmemb;
}

struct CurlResponseContext {
  std::string body;
  std::map<std::string, std::string> headers;
  long status_code = 0;
  std::string status_text;
};

static size_t CurlHeaderCallback(char* buffer, size_t size,
                                 size_t nmemb,
                                 void* userp) {
  auto* ctx = reinterpret_cast<CurlResponseContext*>(userp);
  size_t total = size * nmemb;
  std::string header_line(buffer, total);
  while (!header_line.empty() &&
         (header_line.back() == '\r' ||
          header_line.back() == '\n')) {
    header_line.pop_back();
  }
  if (header_line.rfind("HTTP/", 0) == 0) {
    std::istringstream iss(header_line);
    std::string http_version;
    iss >> http_version >> ctx->status_code;
    std::getline(iss, ctx->status_text);
    while (!ctx->status_text.empty() &&
           ctx->status_text.front() == ' ') {
      ctx->status_text.erase(ctx->status_text.begin());
    }
    return total;
  }
  auto colon = header_line.find(':');
  if (colon != std::string::npos) {
    std::string name = header_line.substr(0, colon);
    std::string value =
        header_line.substr(colon + 1, std::string::npos);
    while (!value.empty() && value.front() == ' ') {
      value.erase(value.begin());
    }
    ctx->headers[name] = value;
  }
  return total;
}

cs::net::http::Status ToStatus(long code,
                               const std::string& name) {
  using cs::net::http::Http301MovedPermanently;
  using cs::net::http::HTTP_200_OK;
  using cs::net::http::HTTP_201_CREATED;
  using cs::net::http::HTTP_302_FOUND;
  using cs::net::http::HTTP_400_BAD_REQUEST;
  using cs::net::http::HTTP_403_PERMISSION_DENIED;
  using cs::net::http::HTTP_404_NOT_FOUND;
  using cs::net::http::HTTP_500_INTERNAL_SERVER_ERROR;
  switch (code) {
    case 200:
      return HTTP_200_OK;
    case 201:
      return HTTP_201_CREATED;
    case 301:
      return Http301MovedPermanently;
    case 302:
      return HTTP_302_FOUND;
    case 400:
      return HTTP_400_BAD_REQUEST;
    case 403:
      return HTTP_403_PERMISSION_DENIED;
    case 404:
      return HTTP_404_NOT_FOUND;
    case 500:
      return HTTP_500_INTERNAL_SERVER_ERROR;
    default:
      return cs::net::http::Status(
          static_cast<uint32_t>(code),
          name.size() > 0 ? name : "UNKNOWN");
  }
}
}  // namespace

ResultOr<std::string> Fetch(
    const std::string& url, const std::string& method,
    const std::map<std::string, std::string>& headers,
    const std::string& body) {
  LOG(DEBUG) << "Fetching URL: " << url
             << " with method: " << method << ENDL;
  CURL* curl = curl_easy_init();
  if (!curl) return Error("Failed to initialize curl");

  std::string response;
  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
                   CurlWriteCallback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

  if (method != "GET") {
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
                     method.c_str());
    if (!body.empty()) {
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
                       body.c_str());
      curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
                       body.size());
    }
  }

  // User-Agent: CoolBot/0.0 (https://example.org/coolbot/;
  // coolbot@example.org) generic-library/0.0
  curl_easy_setopt(curl, CURLOPT_USERAGENT,
                   "GptBot/0.0 (https://cs.p13i.io; "
                   "no-reply@p13i.io) generic-library/0.0");
  // Accept-Encoding: gzip
  //   curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING,
  //   "gzip");

  // Set timeout to 30 seconds
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);

  struct curl_slist* header_list = nullptr;
  for (const auto& [k, v] : headers) {
    header_list = curl_slist_append(header_list,
                                    (k + ": " + v).c_str());
  }
  if (header_list)
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

  CURLcode res = curl_easy_perform(curl);
  if (res != CURLE_OK) {
    std::string err = curl_easy_strerror(res);
    curl_slist_free_all(header_list);
    curl_easy_cleanup(curl);
    LOG(DEBUG) << "Fetch response error (hostname=" << url
               << ") : " << err << ENDL;
    return Error(err);
  }

  LOG(DEBUG) << "Fetch response body: "
             << response.substr(0, 64) << "..." << ENDL;

  curl_slist_free_all(header_list);
  curl_easy_cleanup(curl);
  return response;
}

ResultOr<cs::net::http::Response> FetchResponse(
    const std::string& url, const std::string& method,
    const std::map<std::string, std::string>& headers,
    const std::string& body) {
  LOG(DEBUG) << "Fetching URL: " << url
             << " with method: " << method << ENDL;

  CURL* curl = curl_easy_init();
  if (!curl) return Error("Failed to initialize curl");

  CurlResponseContext context;
  curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
                   CurlWriteCallback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, &context.body);
  curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,
                   CurlHeaderCallback);
  curl_easy_setopt(curl, CURLOPT_HEADERDATA, &context);
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);

  if (method == "GET") {
    curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
  } else {
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST,
                     method.c_str());
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
                     body.c_str());
    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,
                     body.size());
  }

  curl_easy_setopt(curl, CURLOPT_USERAGENT,
                   "GptBot/0.0 (https://cs.p13i.io; "
                   "no-reply@p13i.io) generic-library/0.0");

  // Set timeout to 30 seconds
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L);
  curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);

  struct curl_slist* header_list = nullptr;
  for (const auto& [k, v] : headers) {
    header_list = curl_slist_append(header_list,
                                    (k + ": " + v).c_str());
  }
  if (header_list)
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

  CURLcode res = curl_easy_perform(curl);
  long code_from_info = 0;
  curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
                    &code_from_info);

  if (res != CURLE_OK) {
    std::string err = curl_easy_strerror(res);
    curl_slist_free_all(header_list);
    curl_easy_cleanup(curl);
    return Error(err);
  }

  LOG(DEBUG) << "FetchResponse body: "
             << context.body.substr(0, 64) << "..." << ENDL;

  curl_slist_free_all(header_list);
  curl_easy_cleanup(curl);

  if (context.status_code == 0 && code_from_info != 0) {
    context.status_code = code_from_info;
  }
  if (context.status_code == 0) {
    context.status_code = 500;
    context.status_text = "INTERNAL SERVER ERROR";
  }

  cs::net::http::Status status =
      ToStatus(context.status_code, context.status_text);
  std::string content_type =
      cs::net::http::kContentTypeTextPlain;
  auto found = context.headers.find("Content-Type");
  if (found != context.headers.end()) {
    content_type = found->second;
    context.headers.erase(found);
  }

  cs::net::http::Response response(status, content_type,
                                   context.body);
  response._headers = context.headers;
  return response;
}

}  // namespace cs
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-1:8080 in 6ms.