2 #include <samchon/API.hpp> 5 #include <boost/asio.hpp> 6 #include <samchon/ByteArray.hpp> 7 #include <samchon/library/URLVariables.hpp> 12 #include <samchon/library/Date.hpp> 13 #include <samchon/library/StringUtil.hpp> 107 auto getCookie(
const std::string &key)
const -> std::string
137 if (host.
find(
"://") != std::string::npos)
139 if (host.
find(
"/") != std::string::npos)
141 path =
"/" + host.
between(
"/").str();
147 size_t idx = path.find(
'?');
148 if (idx == std::string::npos)
152 std::string &front = path.substr(0, idx);
153 std::string &back = path.substr(idx + 1);
165 "GET {2}{3} HTTP/1.1\n" +
168 "Accept-Encoding: gzip, deflate\n" 170 "Connection: Keep-Alive\n" +
175 ((parameters.empty() ==
true)
177 :
"?" + parameters.toString()),
183 std::string ¶meterStr = parameters.toString();
188 "POST {2} HTTP/1.1\n" +
191 "Connection: Keep-Alive\n" +
197 "Content-Type: application/x-www-form-urlencoded\n" +
198 "Content-Length: {3}\n" +
204 parameterStr.size(), parameterStr,
210 boost::asio::io_service ioService;
212 boost::asio::ip::tcp::resolver resolver(ioService);
213 boost::asio::ip::tcp::resolver::query query(host,
"http");
214 boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
217 boost::asio::ip::tcp::socket socket(ioService);
218 socket.connect(*endpoint_iterator);
220 socket.write_some(boost::asio::buffer(header));
231 std::array<char, 1> buffer;
233 size_t piece_size = socket.read_some(boost::asio::buffer(buffer));
236 if (header.size() > 4 && header.substr(header.size() - 4) ==
"\r\n\r\n")
241 std::vector<WeakString> wstrArray = wstr.
split(
"\r\n");
243 for (
size_t i = 0; i < wstrArray.size(); i++)
246 size_t index = wstr.
find(
":");
248 if (index == std::string::npos)
256 if (headerMap.
has(
"Set-Cookie") ==
true)
258 std::string &cookie = headerMap.
get(
"Set-Cookie");
264 bool reserved = headerMap.
has(
"Content-Length");
265 bool chunked = headerMap.
has(
"Transfer-Encoding") && headerMap.
get(
"Transfer-Encoding") ==
"chunked";
272 if (reserved ==
true)
275 data.reserve((
size_t)stoull(headerMap.
get(
"Content-Length")));
279 std::array<unsigned char, 1000> piece;
280 boost::system::error_code error;
282 size_t size = socket.read_some(boost::asio::buffer(piece), error);
283 if (size == 0 || error)
289 piece.begin(), piece.begin() + size
292 if (data.size() == data.capacity())
296 else if (chunked ==
true)
298 std::vector<unsigned char> prevData;
302 std::array<char, 1000> piece;
303 boost::system::error_code error;
305 size_t size = socket.read_some(boost::asio::buffer(piece), error);
306 if (size == 0 || error)
309 prevData.insert(prevData.end(), piece.begin(), piece.begin() + size);
312 WeakString wstr((
const char*)&prevData[0], (
const char*)&prevData[0] + prevData.size());
314 if (wstr.substring(wstr.size() - 7, wstr.size()) ==
"\r\n0\r\n\r\n")
316 size_t startIndex = 0;
321 size_t pos = wstr.
find(
"\r\n", startIndex);
324 size_t size = stoull(piece.
str(), 0, 16);
328 startIndex = pos + 2;
329 endIndex = (startIndex + size < prevData.size()) ? startIndex + size : prevData.size();
331 data.insert(data.end(), prevData.begin() + startIndex, prevData.begin() + endIndex);
332 startIndex = endIndex + 2;
343 std::array<unsigned char, 1000> piece;
344 boost::system::error_code error;
346 size_t size = socket.read_some(boost::asio::buffer(piece), error);
347 if (size == 0 || error)
353 piece.begin(), piece.begin() + size
auto getCookie(const std::string &key) const -> std::string
Get cookie.
auto str() const -> std::string
Get the string content.
auto has(const Key &key) const -> bool
Whether have the item or not.
auto get(const Key &key) -> T &
Get element.
static auto substitute(const std::string &format, const T &val, const _Args &...args) -> std::string
Substitutes "{n}" tokens within the specified string with the respective arguments passed in...
static auto encode(const WeakString &wstr) -> std::string
Encode a string into a valid URI.
auto between(const WeakString &start={}, const WeakString &end={}) const -> WeakString
Generates a substring.
int method
Method, Get or Post.
auto getURL() const -> std::string
Get url.
static HashMap< std::string, std::string > & cookie_map()
Cookies got from remote web server.
HTTPLoader(const std::string &url, int method=POST)
Construct from request url and method.
auto find(const WeakString &delim, size_t startIndex=NULL) const -> size_t
Finds first occurence in string.
auto getMethod() const -> int
Get method.
auto load(const URLVariables ¶meters={}) const -> ByteArray
Load data from target url.
auto substr(size_t startIndex, size_t size=SIZE_MAX) const -> WeakString
Generates a substring.
void set(const Key &key, const T &val)
Set element.
auto split(const WeakString &delim) const -> std::vector< WeakString >
Generates substrings.
void setURL(const std::string &val)
Set url.
A string class only references characeters, reference only.
URLVariables class is for representing variables of HTTP.
void setMethod(int val)
Set method.