Support file upload in server side.

This commit is contained in:
Chunting Gu
2019-04-11 17:35:09 +08:00
parent 5f9532759e
commit 5c465a1e2d
22 changed files with 828 additions and 196 deletions
+10 -4
View File
@@ -22,15 +22,21 @@ set(REST_BOOK_SRCS
)
add_executable(http_client http_client.cc)
add_executable(github_client github_client.cc)
target_link_libraries(http_client ${EXAMPLE_COMMON_LIBS})
add_executable(github_client github_client.cc)
target_link_libraries(github_client ${EXAMPLE_COMMON_LIBS} jsoncpp)
add_executable(rest_book_server rest_book_server.cc ${REST_BOOK_SRCS})
add_executable(rest_book_client rest_book_client.cc ${REST_BOOK_SRCS})
add_executable(file_upload_client file_upload_client.cc)
target_link_libraries(file_upload_client ${EXAMPLE_COMMON_LIBS})
add_executable(file_upload_server file_upload_server.cc)
target_link_libraries(file_upload_server ${EXAMPLE_COMMON_LIBS})
add_executable(rest_book_server rest_book_server.cc ${REST_BOOK_SRCS})
target_link_libraries(rest_book_server ${EXAMPLE_COMMON_LIBS} jsoncpp)
add_executable(rest_book_client rest_book_client.cc ${REST_BOOK_SRCS})
target_link_libraries(rest_book_client ${EXAMPLE_COMMON_LIBS} jsoncpp)
if(WEBCC_ENABLE_SOAP)
+55
View File
@@ -0,0 +1,55 @@
#include <iostream>
#include "boost/filesystem.hpp"
#include "webcc/http_client_session.h"
#include "webcc/logger.h"
#if (defined(WIN32) || defined(_WIN64))
// You need to set environment variable SSL_CERT_FILE properly to enable
// SSL verification.
bool kSslVerify = false;
#else
bool kSslVerify = true;
#endif
void Help(const char* argv0) {
std::cout << "Usage: " << argv0 << " <upload_dir>" << std::endl;
std::cout << " E.g.," << std::endl;
std::cout << " " << argv0 << "E:/github/webcc/data/upload" << std::endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
Help(argv[0]);
return 1;
}
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
const webcc::Path upload_dir(argv[1]);
namespace bfs = boost::filesystem;
if (!bfs::is_directory(upload_dir) || !bfs::exists(upload_dir)) {
std::cerr << "Invalid upload dir!" << std::endl;
return 1;
}
webcc::HttpClientSession session;
//std::string url = "http://httpbin.org/post";
std::string url = "http://localhost:8080/upload";
try {
auto r = session.PostFile(url, "file",
upload_dir / "remember.txt");
//std::cout << r->content() << std::endl;
} catch (const webcc::Exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}
+63
View File
@@ -0,0 +1,63 @@
#include <iostream>
#include <string>
#include "webcc/logger.h"
#include "webcc/rest_server.h"
#include "webcc/rest_service.h"
// -----------------------------------------------------------------------------
class FileUploadService : public webcc::RestService {
public:
void Handle(const webcc::RestRequest& request,
webcc::RestResponse* response) final {
if (request.http->method() == webcc::http::methods::kPost) {
std::cout << "files: " << request.http->files().size() << std::endl;
for (auto& pair : request.http->files()) {
std::cout << "name: " << pair.first << std::endl;
std::cout << "data: " << std::endl << pair.second.data() << std::endl;
}
response->content = "OK";
response->media_type = webcc::http::media_types::kTextPlain;
response->charset = "utf-8";
response->status = webcc::http::Status::kCreated;
}
}
};
// -----------------------------------------------------------------------------
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]);
return 1;
}
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
std::uint16_t port = static_cast<std::uint16_t>(std::atoi(argv[1]));
std::size_t workers = 2;
try {
webcc::RestServer server(port, workers);
server.Bind(std::make_shared<FileUploadService>(), "/upload", false);
server.Run();
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return 1;
}
return 0;
}
+2 -2
View File
@@ -198,8 +198,6 @@ int main(int argc, char* argv[]) {
return 1;
}
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE_FILE_OVERWRITE);
std::string url = argv[1];
int timeout = 0;
@@ -207,6 +205,8 @@ int main(int argc, char* argv[]) {
timeout = std::atoi(argv[2]);
}
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE_FILE_OVERWRITE);
// Share the same session.
webcc::HttpClientSession session;