Post a file with data streaming

This commit is contained in:
Chunting Gu
2019-08-12 14:06:59 +08:00
parent 2e655efdc1
commit 4f9263048f
10 changed files with 266 additions and 209 deletions
+7 -10
View File
@@ -6,18 +6,15 @@
#include "webcc/client_session.h"
#include "webcc/logger.h"
void Help(const char* argv0) {
std::cout << "Usage: file_downloader <url> <path>" << std::endl;
std::cout << "E.g.," << std::endl;
std::cout << " file_downloader http://httpbin.org/image/jpeg D:/test.jpg"
<< std::endl;
std::cout << " file_downloader https://www.google.com/favicon.ico"
<< " D:/test.ico" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc != 3) {
Help(argv[0]);
std::cout << "usage: file_downloader <url> <path>" << std::endl;
std::cout << std::endl;
std::cout << "examples:" << std::endl;
std::cout << " $ file_downloader http://httpbin.org/image/jpeg D:/test.jpg"
<< std::endl;
std::cout << " $ file_downloader https://www.google.com/favicon.ico"
<< " D:/test.ico" << std::endl;
return 1;
}
+6 -9
View File
@@ -6,17 +6,14 @@
#include "webcc/logger.h"
#include "webcc/server.h"
void Help() {
std::cout << "Usage:" << std::endl;
std::cout << " file_server <port> <doc_root> [chunk_size]" << std::endl;
std::cout << "E.g.," << std::endl;
std::cout << " file_server 8080 D:/www" << std::endl;
std::cout << " file_server 8080 D:/www 10000" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
Help();
std::cout << "usage: file_server <port> <doc_root> [chunk_size]"
<< std::endl;
std::cout << std::endl;
std::cout << "examples:" << std::endl;
std::cout << " $ file_server 8080 D:/www" << std::endl;
std::cout << " $ file_server 8080 D:/www 10000" << std::endl;
return 1;
}
+11 -14
View File
@@ -5,20 +5,17 @@
#include "webcc/client_session.h"
#include "webcc/logger.h"
void Help() {
std::cout << "Usage:" << std::endl;
std::cout << " file_upload_client <upload_dir> [url]" << std::endl;
std::cout << "Default Url: http://httpbin.org/post" << std::endl;
std::cout << "E.g.," << std::endl;
std::cout << " file_upload_client E:/github/webcc/data/upload"
<< std::endl;
std::cout << " file_upload_client E:/github/webcc/data/upload"
<< " http://httpbin.org/post" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
Help();
std::cout << "usage: file_upload_client <upload_dir> [url]" << std::endl;
std::cout << std::endl;
std::cout << "default url: http://httpbin.org/post" << std::endl;
std::cout << std::endl;
std::cout << "examples:" << std::endl;
std::cout << " $ file_upload_client E:/github/webcc/data/upload"
<< std::endl;
std::cout << " $ file_upload_client E:/github/webcc/data/upload "
<< "http://httpbin.org/post" << std::endl;
return 1;
}
@@ -45,8 +42,8 @@ int main(int argc, char* argv[]) {
try {
auto r = session.Request(webcc::RequestBuilder{}.
Post(url).
File("file", upload_dir / "remember.txt").
Form("json", "{}", "application/json")
FormFile("file", upload_dir / "remember.txt").
FormData("json", "{}", "application/json")
());
std::cout << r->status() << std::endl;
+1 -7
View File
@@ -32,15 +32,9 @@ private:
// -----------------------------------------------------------------------------
void Help(const char* argv0) {
std::cout << "Usage: " << argv0 << " <port>" << std::endl;
std::cout << " E.g.," << std::endl;
std::cout << " " << argv0 << " 8080" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
Help(argv[0]);
std::cout << "usage: file_upload_server <port>" << std::endl;
return 1;
}
+5 -9
View File
@@ -188,17 +188,13 @@ void PrintBookList(const std::list<Book>& books) {
// -----------------------------------------------------------------------------
void Help() {
std::cout << "Usage:" << std::endl;
std::cout << " rest_book_client <url> [timeout]" << std::endl;
std::cout << "E.g.," << std::endl;
std::cout << " rest_book_client http://localhost:8080" << std::endl;
std::cout << " rest_book_client http://localhost:8080 2" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
Help();
std::cout << "usage: rest_book_client <url> [timeout]" << std::endl;
std::cout << std::endl;
std::cout << "examples:" << std::endl;
std::cout << " $ rest_book_client http://localhost:8080" << std::endl;
std::cout << " $ rest_book_client http://localhost:8080 2" << std::endl;
return 1;
}
+13 -15
View File
@@ -145,8 +145,8 @@ webcc::ResponsePtr BookDetailView::Get(webcc::RequestPtr request) {
Sleep(sleep_seconds_);
if (request->args().size() != 1) {
// Using kNotFound means the resource specified by the URL cannot be found.
// kBadRequest could be another choice.
// NotFound means the resource specified by the URL cannot be found.
// BadRequest could be another choice.
return webcc::ResponseBuilder{}.NotFound()();
}
@@ -157,8 +157,8 @@ webcc::ResponsePtr BookDetailView::Get(webcc::RequestPtr request) {
return webcc::ResponseBuilder{}.NotFound()();
}
return webcc::ResponseBuilder{}.OK().Body(BookToJsonString(book)).Json().
Utf8()();
return webcc::ResponseBuilder{}.OK().Body(BookToJsonString(book)).
Json().Utf8()();
}
webcc::ResponsePtr BookDetailView::Put(webcc::RequestPtr request) {
@@ -199,19 +199,17 @@ webcc::ResponsePtr BookDetailView::Delete(webcc::RequestPtr request) {
// -----------------------------------------------------------------------------
void Help(const char* argv0) {
std::cout << "Usage: " << argv0 << " <port> [seconds]" << std::endl;
std::cout << "If |seconds| is provided, the server will sleep these seconds "
"before sending back each response."
<< std::endl;
std::cout << " E.g.," << std::endl;
std::cout << " " << argv0 << " 8080" << std::endl;
std::cout << " " << argv0 << " 8080 3" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
Help(argv[0]);
std::cout << "usage: rest_book_server <port> [seconds]" << std::endl;
std::cout << std::endl;
std::cout << "If |seconds| is provided, the server will sleep, for testing "
<< "timeout, before " << std::endl
<< "send back each response." << std::endl;
std::cout << std::endl;
std::cout << "examples:" << std::endl;
std::cout << " $ rest_book_server 8080" << std::endl;
std::cout << " $ rest_book_server 8080 3" << std::endl;
return 1;
}