Skip to content

Commit 8382e77

Browse files
committed
setDownloadBandwidthLimit and setUploadBandwidthLimit methods have been added
1 parent 7437a56 commit 8382e77

File tree

3 files changed

+306
-227
lines changed

3 files changed

+306
-227
lines changed

examples/main.cpp

Lines changed: 92 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33

44
using namespace lklibs;
55

6-
void simpleGet() {
7-
6+
void simpleGet()
7+
{
88
HttpRequest httpRequest("https://httpbun.com/get");
99

1010
// The simplest but slowest method if multiple calls will be made
1111
auto response = httpRequest
12-
.setQueryString("param1=7&param2=test")
13-
.send()
14-
.get();
12+
.setQueryString("param1=7&param2=test")
13+
.send()
14+
.get();
1515

1616
std::cout << "Succeed: " << response.succeed << std::endl;
1717
std::cout << "Http Status Code: " << response.statusCode << std::endl;
1818
std::cout << "Data: " << response.textData << std::endl;
1919
}
2020

21-
void nonBlockingGet() {
22-
21+
void nonBlockingGet()
22+
{
2323
HttpRequest httpRequest1("https://httpbun.com/get");
2424
HttpRequest httpRequest2("https://httpbun.com/get");
2525
HttpRequest httpRequest3("https://httpbun.com/get");
@@ -47,15 +47,15 @@ void nonBlockingGet() {
4747
std::cout << "Response3 Data: " << response3.textData << std::endl;
4848
}
4949

50-
void receiveBinaryData() {
51-
50+
void receiveBinaryData()
51+
{
5252
HttpRequest httpRequest("https://httpbun.com/bytes/100");
5353

5454
// If you need to retrieve binary data such as an image, just call the "returnAsBinary" method before send
5555
auto response = httpRequest
56-
.returnAsBinary()
57-
.send()
58-
.get();
56+
.returnAsBinary()
57+
.send()
58+
.get();
5959

6060
std::cout << "Succeed: " << response.succeed << std::endl;
6161
std::cout << "Http Status Code: " << response.statusCode << std::endl;
@@ -64,8 +64,8 @@ void receiveBinaryData() {
6464
std::cout << "Data Size: " << response.binaryData.size() << std::endl;
6565
}
6666

67-
void receiveError() {
68-
67+
void receiveError()
68+
{
6969
HttpRequest httpRequest("https://httpbun.com/not_found");
7070

7171
// This is an exception free library. If an error occurs, no exception is thrown
@@ -81,103 +81,103 @@ void receiveError() {
8181
std::cout << "Error Message: " << response.errorMessage << std::endl;
8282
}
8383

84-
void sendingHttpHeaders() {
85-
84+
void sendingHttpHeaders()
85+
{
8686
HttpRequest httpRequest("https://httpbun.com/get?param1=7&param2=test");
8787

8888
// You can send custom headers as key-value pairs
8989
auto response = httpRequest
90-
.addHeader("Custom-Header1", "value1")
91-
.addHeader("Custom-Header2", "value2")
92-
.send()
93-
.get();
90+
.addHeader("Custom-Header1", "value1")
91+
.addHeader("Custom-Header2", "value2")
92+
.send()
93+
.get();
9494

9595
std::cout << "Succeed: " << response.succeed << std::endl;
9696
}
9797

98-
void simplePostWithFormData() {
99-
98+
void simplePostWithFormData()
99+
{
100100
HttpRequest httpRequest("https://httpbun.com/post");
101101

102102
// You can send a POST request with form data in the payload
103103
auto response = httpRequest
104-
.setMethod(HttpMethod::POST)
105-
.setPayload("param1=7&param2=test")
106-
.send()
107-
.get();
104+
.setMethod(HttpMethod::POST)
105+
.setPayload("param1=7&param2=test")
106+
.send()
107+
.get();
108108

109109
std::cout << "Succeed: " << response.succeed << std::endl;
110110
std::cout << "Http Status Code: " << response.statusCode << std::endl;
111111
std::cout << "Data: " << response.textData << std::endl;
112112
}
113113

114-
void simplePostWithJSONData() {
115-
114+
void simplePostWithJSONData()
115+
{
116116
HttpRequest httpRequest("https://httpbun.com/post");
117117

118118
// You need to send the "Content-Type" as "application/json" in the HTTP Header, if you need to send json data in the payload
119119
auto response = httpRequest
120-
.setMethod(HttpMethod::POST)
121-
.setPayload(R"({"param1": 7, "param2": "test"})")
122-
.addHeader("Content-Type", "application/json")
123-
.send()
124-
.get();
120+
.setMethod(HttpMethod::POST)
121+
.setPayload(R"({"param1": 7, "param2": "test"})")
122+
.addHeader("Content-Type", "application/json")
123+
.send()
124+
.get();
125125

126126
std::cout << "Succeed: " << response.succeed << std::endl;
127127
std::cout << "Http Status Code: " << response.statusCode << std::endl;
128128
std::cout << "Data: " << response.textData << std::endl;
129129
}
130130

131-
void simplePutWithFormData() {
132-
131+
void simplePutWithFormData()
132+
{
133133
HttpRequest httpRequest("https://httpbun.com/put");
134134

135135
// You can send a PUT request with form data in the payload just like POST
136136
auto response = httpRequest
137-
.setMethod(HttpMethod::PUT)
138-
.setPayload("param1=7&param2=test")
139-
.send()
140-
.get();
137+
.setMethod(HttpMethod::PUT)
138+
.setPayload("param1=7&param2=test")
139+
.send()
140+
.get();
141141

142142
std::cout << "Succeed: " << response.succeed << std::endl;
143143
std::cout << "Http Status Code: " << response.statusCode << std::endl;
144144
std::cout << "Data: " << response.textData << std::endl;
145145
}
146146

147-
void simpleDeleteWithFormData() {
148-
147+
void simpleDeleteWithFormData()
148+
{
149149
HttpRequest httpRequest("https://httpbun.com/delete");
150150

151151
// You can send a DELETE request with form data in the payload just like POST
152152
auto response = httpRequest
153-
.setMethod(HttpMethod::DELETE_)
154-
.setPayload("param1=7&param2=test")
155-
.send()
156-
.get();
153+
.setMethod(HttpMethod::DELETE_)
154+
.setPayload("param1=7&param2=test")
155+
.send()
156+
.get();
157157

158158
std::cout << "Succeed: " << response.succeed << std::endl;
159159
std::cout << "Http Status Code: " << response.statusCode << std::endl;
160160
std::cout << "Data: " << response.textData << std::endl;
161161
}
162162

163-
void simplePatch() {
164-
163+
void simplePatch()
164+
{
165165
HttpRequest httpRequest("https://httpbun.com/patch");
166166

167167
// You can send a PATCH request with QueryString just like GET
168168
auto response = httpRequest
169-
.setMethod(HttpMethod::PATCH)
170-
.setQueryString("param1=7&param2=test")
171-
.send()
172-
.get();
169+
.setMethod(HttpMethod::PATCH)
170+
.setQueryString("param1=7&param2=test")
171+
.send()
172+
.get();
173173

174174
std::cout << "Succeed: " << response.succeed << std::endl;
175175
std::cout << "Http Status Code: " << response.statusCode << std::endl;
176176
std::cout << "Data: " << response.textData << std::endl;
177177
}
178178

179-
void ignoreSslErrors() {
180-
179+
void ignoreSslErrors()
180+
{
181181
HttpRequest httpRequest("https://self-signed-cert.httpbun.com");
182182

183183
// If you need to ignore SSL errors, you can call "ignoreSslErrors" method before sending the request
@@ -188,30 +188,48 @@ void ignoreSslErrors() {
188188
std::cout << "Data: " << response.textData << std::endl;
189189
}
190190

191+
void setUploadAndDownloadBandwidthLimit()
192+
{
193+
HttpRequest httpRequest("https://httpbun.com/get");
191194

192-
int main() {
193-
194-
simpleGet();
195-
196-
nonBlockingGet();
197-
198-
receiveBinaryData();
199-
200-
receiveError();
201-
202-
sendingHttpHeaders();
203-
204-
simplePostWithFormData();
205-
206-
simplePostWithJSONData();
207-
208-
simplePutWithFormData();
195+
// You can set the upload and download bandwidth limit in bytes per second
196+
auto response = httpRequest
197+
.setUploadBandwidthLimit(20480) // 2 KB/sec
198+
.setDownloadBandwidthLimit(10240) // 1 KB/sec
199+
.send()
200+
.get();
209201

210-
simpleDeleteWithFormData();
202+
std::cout << "Succeed: " << response.succeed << std::endl;
203+
std::cout << "Http Status Code: " << response.statusCode << std::endl;
204+
std::cout << "Data: " << response.textData << std::endl;
205+
}
211206

212-
simplePatch();
213207

214-
ignoreSslErrors();
208+
int main()
209+
{
210+
// simpleGet();
211+
//
212+
// nonBlockingGet();
213+
//
214+
// receiveBinaryData();
215+
//
216+
// receiveError();
217+
//
218+
// sendingHttpHeaders();
219+
//
220+
// simplePostWithFormData();
221+
//
222+
// simplePostWithJSONData();
223+
//
224+
// simplePutWithFormData();
225+
//
226+
// simpleDeleteWithFormData();
227+
//
228+
// simplePatch();
229+
//
230+
// ignoreSslErrors();
231+
232+
setUploadAndDownloadBandwidthLimit();
215233

216234
return 0;
217-
}
235+
}

src/libcpp-http-client.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,30 @@ namespace lklibs {
224224
return *this;
225225
}
226226

227+
/**
228+
* @brief Set the download bandwidth limit for the request
229+
*
230+
* @param limit: Download bandwidth limit in bytes per second (0 for no limit)
231+
*/
232+
HttpRequest &setDownloadBandwidthLimit(int limit) noexcept {
233+
234+
this->downloadBandwidthLimit = limit;
235+
236+
return *this;
237+
}
238+
239+
/**
240+
* @brief Set the upload bandwidth limit for the request
241+
*
242+
* @param limit: Upload bandwidth limit in bytes per second (0 for no limit)
243+
*/
244+
HttpRequest &setUploadBandwidthLimit(int limit) noexcept {
245+
246+
this->uploadBandwidthLimit = limit;
247+
248+
return *this;
249+
}
250+
227251
/**
228252
* @brief Send the HTTP request and return the result as a future
229253
* The result can be obtained by calling the get() method of the future
@@ -258,6 +282,8 @@ namespace lklibs {
258282
bool sslErrorsWillBeIgnored = false;
259283
ReturnFormat returnFormat = ReturnFormat::TEXT;
260284
std::map<std::string, std::string> headers;
285+
int uploadBandwidthLimit = 0;
286+
int downloadBandwidthLimit = 0;
261287

262288
struct CurlDeleter {
263289

@@ -310,6 +336,9 @@ namespace lklibs {
310336
curl_easy_setopt(curl.get(), CURLOPT_CUSTOMREQUEST, this->method.c_str());
311337
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYPEER, this->sslErrorsWillBeIgnored ? 0L : 1L);
312338
curl_easy_setopt(curl.get(), CURLOPT_SSL_VERIFYHOST, this->sslErrorsWillBeIgnored ? 0L : 1L);
339+
curl_easy_setopt(curl.get(), CURLOPT_MAX_SEND_SPEED_LARGE, this->uploadBandwidthLimit);
340+
curl_easy_setopt(curl.get(), CURLOPT_MAX_RECV_SPEED_LARGE, this->downloadBandwidthLimit);
341+
313342

314343
if (!this->payload.empty()) {
315344

0 commit comments

Comments
 (0)