Lolly 1.4.27
Loading...
Searching...
No Matches
http.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : http.cpp
4 * DESCRIPTION: HTTP related routines
5 * COPYRIGHT : (C) 2023 Darcy Shen
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "lolly/io/http.hpp"
13#include "analyze.hpp"
14#include "hashmap.hpp"
15#include "iterator.hpp"
16#include "lolly/data/uri.hpp"
17
18#ifndef OS_WASM
19#include <cpr/cpr.h>
20#endif
21
22namespace lolly {
23namespace io {
24
25const char* HTTP_USER_AGENT= "User-Agent";
26
27template <typename T>
28inline http_tree
29blackbox_tree (int label, T data) {
30 http_tree ret= http_tree (label);
31 ret->data = close_box<T> (data);
32 return ret;
33}
34
35#ifdef OS_WASM
36
39 return http_response_init ();
40}
41
44 return http_response_init ();
45}
46
49 return http_response_init ();
50}
51
52#else
53
54static http_tree
61 ret, TEXT,
62 blackbox_tree<string> (http_label::TEXT, string (r.text.c_str ())));
67
69 for (auto i= r.header.begin (); i != r.header.end (); i++) {
70 string key = locase_all (string (i->first.c_str ()));
71 string value= string (i->second.c_str ());
72 hmap (key) = value;
73 }
77 return ret;
78}
79
80static cpr::Header
84 while (it->busy ()) {
85 string key = it->next ();
86 header[std::string (c_string (key))]= c_string (hmap[key]);
87 }
88 return header;
89}
90
93 string u_str = as_string (u);
96 session.SetUrl (cpr::Url (u_cstr));
97 session.SetHeader (as_cpr_header (headers));
98 session.SetUserAgent (
100 cpr::Response r= session.Get ();
101 return response_to_tree (r, u_str);
102}
103
106 string u_str = as_string (u);
109 session.SetUrl (cpr::Url (u_cstr));
110 session.SetHeader (as_cpr_header (headers));
111 session.SetUserAgent (
113 cpr::Response r= session.Head ();
114 return response_to_tree (r, u_str);
115}
116
119 string from_str = as_string (from);
121 string to_str = as_string (to);
123
125 session.SetUrl (cpr::Url (from_cstr));
126 session.SetHeader (as_cpr_header (headers));
127 session.SetUserAgent (
129 std::ofstream to_stream (to_cstr, std::ios::binary);
130 cpr::Response r= session.Download (to_stream);
131 return response_to_tree (r, from_str);
132}
133
134#endif
135
136} // namespace io
137} // namespace lolly
string locase_all(string s)
Converts all uppercase characters in a string to lowercase.
Definition analyze.cpp:137
A simple hashmap class implementation.
Definition hashmap.hpp:176
The list class represents a linked list.
Definition list.hpp:48
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
Definition url.hpp:37
iterator< T > iterate(hashmap< T, U > h)
Generates an iterator for a container of type hashmap<T, U>.
Definition iterator.ipp:166
http_tree download(url from, url to, http_headers headers)
Definition http.cpp:118
static http_tree response_to_tree(cpr::Response r, string url)
Definition http.cpp:55
lolly_tree< blackbox > http_tree
Definition http.hpp:22
void http_response_set(http_tree r, http_label op, http_tree t)
Definition http.hpp:60
static cpr::Header as_cpr_header(http_headers hmap)
Definition http.cpp:81
@ HEADER
Definition http.hpp:29
@ ELAPSED
Definition http.hpp:28
@ STATUS_CODE
Definition http.hpp:25
hashmap< string, string > http_headers
Definition http.hpp:37
http_tree blackbox_tree(int label, T data)
Definition http.cpp:29
const char * HTTP_USER_AGENT
Definition http.cpp:25
http_tree http_response_init()
Definition http.hpp:42
http_tree http_head(url u, http_headers headers)
Definition http.cpp:105
http_tree http_get(url u, http_headers headers)
Definition http.cpp:92
string as_string(int16_t i)
Definition string.cpp:310