session
This commit is contained in:
@@ -38,7 +38,8 @@ u8 hex_to_u8(string src)
|
||||
return(char_to_u8(src[0])*16 + char_to_u8(src[1]));
|
||||
}
|
||||
|
||||
template <typename ITYPE> string to_hex(ITYPE w, size_t hex_len = sizeof(ITYPE)<<1)
|
||||
template <typename ITYPE>
|
||||
string to_hex(ITYPE w, size_t hex_len = sizeof(ITYPE)<<1)
|
||||
{
|
||||
static const char* digits = "0123456789ABCDEF";
|
||||
string rc(hex_len,'0');
|
||||
|
||||
@@ -66,6 +66,12 @@ string dirname(string fn)
|
||||
return(result);
|
||||
}
|
||||
|
||||
bool mkdir(string path)
|
||||
{
|
||||
shell_exec(string("mkdir -p ")+" "+shell_escape(path));
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool file_exists(string path)
|
||||
{
|
||||
std::filesystem::path fp{ path };
|
||||
|
||||
+6
-1
@@ -48,9 +48,11 @@ struct ServerSettings {
|
||||
string LIT_ESC = "3d5b5_1";
|
||||
string CONTENT_TYPE = "text/html; charset=utf-8";
|
||||
string SOCKET_PATH = "/run/uce.sock";
|
||||
string TMP_UPLOAD = "/tmp";
|
||||
string TMP_UPLOAD_PATH = "/tmp/uce/uploads";
|
||||
string SESSION_PATH = "/tmp/uce/sessions";
|
||||
string COMPILER_SYS_PATH = "";
|
||||
u32 LISTEN_PORT = 9993;
|
||||
u64 SESSION_TIME = 60*60*24*30;
|
||||
|
||||
};
|
||||
|
||||
@@ -106,6 +108,9 @@ struct Request {
|
||||
StringMap get;
|
||||
StringMap post;
|
||||
StringMap cookies;
|
||||
StringMap session;
|
||||
string session_id = "";
|
||||
string session_name = "";
|
||||
std::vector<UploadedFile> uploaded_files;
|
||||
|
||||
StringMap header;
|
||||
|
||||
+53
-2
@@ -83,6 +83,20 @@ StringMap parse_query(string q)
|
||||
return(result);
|
||||
}
|
||||
|
||||
string encode_query(StringMap map)
|
||||
{
|
||||
string result;
|
||||
|
||||
for (auto it = map.begin(); it != map.end(); ++it)
|
||||
{
|
||||
if(result.length() > 0)
|
||||
result.append(1, '&');
|
||||
result.append(uri_encode(it->first) + "=" + uri_encode(it->second));
|
||||
}
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
StringMap parse_multipart(string q, string boundary, std::vector<UploadedFile>& uploaded_files)
|
||||
{
|
||||
StringMap result;
|
||||
@@ -106,7 +120,7 @@ StringMap parse_multipart(string q, string boundary, std::vector<UploadedFile>&
|
||||
{
|
||||
nibble("=\"", field);
|
||||
UploadedFile f;
|
||||
f.tmp_name = context->server_state->config.TMP_UPLOAD + to_string(rand());
|
||||
f.tmp_name = context->server_state->config.TMP_UPLOAD_PATH + to_string(rand());
|
||||
f.file_name = nibble("\"", field);
|
||||
string bin = field.substr(4, field.length()-6);
|
||||
f.size = bin.length();
|
||||
@@ -248,8 +262,8 @@ void set_cookie(
|
||||
cookie.append(uri_encode(name) + "=" + uri_encode(value));
|
||||
if(expires > 0)
|
||||
cookie.append(string("; Expires=") + gmdate("RFC1123", expires));
|
||||
|
||||
context->set_cookies.push_back(cookie);
|
||||
context->cookies[name] = value;
|
||||
}
|
||||
|
||||
StringMap parse_cookies(string cookie_string)
|
||||
@@ -263,3 +277,40 @@ StringMap parse_cookies(string cookie_string)
|
||||
}
|
||||
return(result);
|
||||
}
|
||||
|
||||
string make_session_id()
|
||||
{
|
||||
return(to_hex(rand())+to_hex(rand())+to_hex(rand())+to_hex(rand()));
|
||||
}
|
||||
|
||||
StringMap load_session_data(string session_id)
|
||||
{
|
||||
return(parse_query(file_get_contents(context->server_state->config.SESSION_PATH + "/" + session_id)));
|
||||
}
|
||||
|
||||
void save_session_data(string session_id, StringMap data)
|
||||
{
|
||||
file_put_contents(context->server_state->config.SESSION_PATH + "/" + session_id, encode_query(data));
|
||||
}
|
||||
|
||||
string session_start(string session_name = "uce-session")
|
||||
{
|
||||
if(context->cookies[session_name].length() == 0)
|
||||
{
|
||||
set_cookie(session_name, make_session_id(), time() + context->server_state->config.SESSION_TIME);
|
||||
}
|
||||
context->session_id = context->cookies[session_name];
|
||||
context->session_name = session_name;
|
||||
context->session = load_session_data(context->session_id);
|
||||
return(context->session_id);
|
||||
}
|
||||
|
||||
void session_destroy(string session_name = "uce-session")
|
||||
{
|
||||
if(context->cookies[session_name].length() > 0)
|
||||
{
|
||||
set_cookie(session_name, "", time() - context->server_state->config.SESSION_TIME);
|
||||
context->session.clear();
|
||||
context->session_id = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +77,9 @@ int handle_complete(FastCGIRequest& request) {
|
||||
unlink(f.tmp_name);
|
||||
}
|
||||
|
||||
if(request.session_id.length() > 0)
|
||||
save_session_data(request.session_id, request.session);
|
||||
|
||||
request.time_end = microtime();
|
||||
printf("(r) %s\t%0.6fs\t%0.1fkB\n",
|
||||
request.params["REQUEST_URI"].c_str(),
|
||||
@@ -90,6 +93,8 @@ FastCGIServer server;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
srand(time());
|
||||
|
||||
// Set up our request handlers
|
||||
server.request_handler(&handle_request);
|
||||
server.data_handler(&handle_data);
|
||||
@@ -110,6 +115,9 @@ int main(int argc, char** argv)
|
||||
dirname(server_state.config.COMPILER_SYS_PATH);
|
||||
basename(server_state.config.COMPILER_SYS_PATH);
|
||||
|
||||
mkdir(server_state.config.TMP_UPLOAD_PATH);
|
||||
mkdir(server_state.config.SESSION_PATH);
|
||||
|
||||
//server.process(100);
|
||||
//server.process();
|
||||
server.process_forever();
|
||||
|
||||
Reference in New Issue
Block a user