A file upload form.This example shows how to implement a file upload.
#include <ehs.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include "common.h"
using namespace std;
class FileUploader :
public EHS {
public:
FileUploader ( ) {}
};
{
string sUri = request->
Uri();
cerr << "Request-URI: " << sUri << endl;
if ( sUri == "/" ) {
string sMsg = request->
Cookies(
"ehs_upload_status");
ostringstream oss;
oss << "<html><head><title>ehs uploader</title></head>" << endl << "<body>" << endl
<< " <form method=\"POST\" action=\"/upload.html\" enctype=\"multipart/form-data\">" << endl
<< " Upload file:<br />" << endl
<< " <input type=\"file\" name=\"file\"><br />" << endl
<< " <input type=\"submit\" value=\"submit\">" << endl
<< " </form>" << endl;
if (!sMsg.empty()) {
oss << " <p>Status: " << sMsg << "<br />" << endl;
}
oss << "</body></html>" << endl;
response->
SetBody(oss.str().c_str(), oss.str().length());
return HTTPRESPONSECODE_200_OK;
}
if (sUri == "/upload.html") {
int nFileSize = request->
FormValues(
"file").m_sBody.length();
m_oContentDisposition.m_oContentDispositionHeaders["filename"];
cerr << "nFileSize = " << nFileSize << endl;
cerr << "sFileName = '" << sFileName << "'" << endl;
CookieParameters statusCookie;
statusCookie["name"] = "ehs_upload_status";
statusCookie["value"] = "";
if (0 != nFileSize) {
size_t lastSlash = sFileName.find_last_of("/\\");
sFileName = sFileName.substr(lastSlash + 1);
cerr << "stripped sFileName = '" << sFileName << "'" << endl;
if (!sFileName.empty()) {
if (0 != access(sFileName.c_str(), F_OK)) {
cerr << "Writing " << nFileSize << " bytes to file" << endl;
ofstream outfile(sFileName.c_str(), ios::out | ios::trunc | ios::binary );
outfile.write(request->
FormValues(
"file").m_sBody.c_str(), nFileSize);
outfile.close();
statusCookie["value"] = "Uploaded file successfully.";
} else {
statusCookie["value"] = "No permission to overwrite a file.";
}
} else {
statusCookie["value"] = "No filename received.";
cerr << "NO FILENAME FOUND" << endl;
}
} else {
statusCookie["value"] = "No content received.";
}
return HTTPRESPONSECODE_301_MOVEDPERMANENTLY;
}
return HTTPRESPONSECODE_404_NOTFOUND;
}
int main(int argc, char ** argv)
{
cout << getEHSconfig() << endl;
if (argc < 2) {
cout << "usage: " << basename(argv[0]) << " <port>" << endl;
return 0;
}
FileUploader srv;
EHSServerParameters oSP;
oSP["port"] = argv[1];
oSP["mode"] = "threadpool";
oSP["maxrequestsize"] = 1024 * 1024 * 10;
try {
srv.StartServer(oSP);
kbdio kbd;
cout << "Press q to terminate ..." << endl;
while (!(srv.ShouldTerminate() || kbd.qpressed())) {
usleep(300000);
}
srv.StopServer();
} catch (exception &e) {
cerr << "ERROR: " << e.what() << endl;
}
return 0;
}
EHS provides HTTP server functionality to a child class.
This class represents a clients HTTP request.
FormValueMap & FormValues()
Retrieves form values.
const std::string & Uri() const
Retrieves this request's URI.
CookieMap & Cookies()
Retrieves cookies.
This class represents what is sent back to the client.
void SetBody(const char *ipsBody, size_t inBodyLength)
Sets the body of this instance.
void SetHeader(const std::string &name, const std::string &value)
Sets an HTTP header.
void SetCookie(CookieParameters &iroCookieParameters)
Sets cookies for this response.