This commit is contained in:
Udo 2021-10-30 18:23:04 +00:00
parent af3f2a1f96
commit 0541b77fe1
10 changed files with 137 additions and 11 deletions

Binary file not shown.

View File

@ -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');

View File

@ -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 };

View File

@ -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;

View File

@ -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 = "";
}
}

View File

@ -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();

View File

@ -9,11 +9,7 @@ RENDER()
<a href="index.uce">UCE Test</a>:
Cookies
</h1>
<pre><?
echo(var_dump(context->cookies));
?></pre>
Set
<pre><?
set_cookie("test-cookie-1", "test-value-1", time() + 30*60);
@ -21,6 +17,13 @@ RENDER()
echo(var_dump(context->set_cookies));
?></pre>
Get
<pre><?
echo(var_dump(context->cookies));
?></pre>
Params
<pre><?= var_dump(context->params) ?></pre>
</>

View File

@ -16,6 +16,7 @@ RENDER()
<li><a href="working-dir.uce">Working Directory</a></li>
<li><a href="uri.uce">URI</a></li>
<li><a href="cookie.uce">Cookies</a></li>
<li><a href="session.uce">Session</a></li>
</ul>
<pre><?= var_dump(context->params) ?></pre>
</>

52
test/session.uce Normal file
View File

@ -0,0 +1,52 @@
RENDER()
{
<>
<link rel="stylesheet" href='style.css'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Session
</h1>
</>
string action = context->get["action"];
if(context->cookies["uce-session"].length() > 0)
session_start();
if(action == "start")
{
session_start();
echo("action: starting session");
}
else if(action == "store")
{
context->session["stored-value"] = make_session_id();
echo("action: storing value "+context->session["stored-value"]);
}
else if(action == "destroy")
{
session_destroy();
echo("action: destroying session");
}
<>
<br/>
<a href="?action=start" class="button">Start Session</a> |
<a href="?" class="button">Refresh</a> |
<a href="?action=destroy" class="button">Destroy Session</a> |
<a href="?action=store" class="button">Store Value</a> |
Info
<pre><?
echo(var_dump(context->cookies));
echo("SESSION:\n");
echo(var_dump(context->session));
?></pre>
Params
<pre><?= var_dump(context->params) ?></pre>
</>
}

View File

@ -5,7 +5,6 @@ Library
- sha1 / md5 / meow
- cache
- json_encode / json_decode
- session
- curl
- random
- pseudorandom
@ -20,7 +19,7 @@ Bugs
Framework
=================================
- DTree
- DTree<
- File Upload
- Proxy mode
- WebSockets