session
This commit is contained in:
Binary file not shown.
@@ -38,7 +38,8 @@ u8 hex_to_u8(string src)
|
|||||||
return(char_to_u8(src[0])*16 + char_to_u8(src[1]));
|
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";
|
static const char* digits = "0123456789ABCDEF";
|
||||||
string rc(hex_len,'0');
|
string rc(hex_len,'0');
|
||||||
|
|||||||
@@ -66,6 +66,12 @@ string dirname(string fn)
|
|||||||
return(result);
|
return(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool mkdir(string path)
|
||||||
|
{
|
||||||
|
shell_exec(string("mkdir -p ")+" "+shell_escape(path));
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
bool file_exists(string path)
|
bool file_exists(string path)
|
||||||
{
|
{
|
||||||
std::filesystem::path fp{ path };
|
std::filesystem::path fp{ path };
|
||||||
|
|||||||
+6
-1
@@ -48,9 +48,11 @@ struct ServerSettings {
|
|||||||
string LIT_ESC = "3d5b5_1";
|
string LIT_ESC = "3d5b5_1";
|
||||||
string CONTENT_TYPE = "text/html; charset=utf-8";
|
string CONTENT_TYPE = "text/html; charset=utf-8";
|
||||||
string SOCKET_PATH = "/run/uce.sock";
|
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 = "";
|
string COMPILER_SYS_PATH = "";
|
||||||
u32 LISTEN_PORT = 9993;
|
u32 LISTEN_PORT = 9993;
|
||||||
|
u64 SESSION_TIME = 60*60*24*30;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -106,6 +108,9 @@ struct Request {
|
|||||||
StringMap get;
|
StringMap get;
|
||||||
StringMap post;
|
StringMap post;
|
||||||
StringMap cookies;
|
StringMap cookies;
|
||||||
|
StringMap session;
|
||||||
|
string session_id = "";
|
||||||
|
string session_name = "";
|
||||||
std::vector<UploadedFile> uploaded_files;
|
std::vector<UploadedFile> uploaded_files;
|
||||||
|
|
||||||
StringMap header;
|
StringMap header;
|
||||||
|
|||||||
+53
-2
@@ -83,6 +83,20 @@ StringMap parse_query(string q)
|
|||||||
return(result);
|
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 parse_multipart(string q, string boundary, std::vector<UploadedFile>& uploaded_files)
|
||||||
{
|
{
|
||||||
StringMap result;
|
StringMap result;
|
||||||
@@ -106,7 +120,7 @@ StringMap parse_multipart(string q, string boundary, std::vector<UploadedFile>&
|
|||||||
{
|
{
|
||||||
nibble("=\"", field);
|
nibble("=\"", field);
|
||||||
UploadedFile f;
|
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);
|
f.file_name = nibble("\"", field);
|
||||||
string bin = field.substr(4, field.length()-6);
|
string bin = field.substr(4, field.length()-6);
|
||||||
f.size = bin.length();
|
f.size = bin.length();
|
||||||
@@ -248,8 +262,8 @@ void set_cookie(
|
|||||||
cookie.append(uri_encode(name) + "=" + uri_encode(value));
|
cookie.append(uri_encode(name) + "=" + uri_encode(value));
|
||||||
if(expires > 0)
|
if(expires > 0)
|
||||||
cookie.append(string("; Expires=") + gmdate("RFC1123", expires));
|
cookie.append(string("; Expires=") + gmdate("RFC1123", expires));
|
||||||
|
|
||||||
context->set_cookies.push_back(cookie);
|
context->set_cookies.push_back(cookie);
|
||||||
|
context->cookies[name] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringMap parse_cookies(string cookie_string)
|
StringMap parse_cookies(string cookie_string)
|
||||||
@@ -263,3 +277,40 @@ StringMap parse_cookies(string cookie_string)
|
|||||||
}
|
}
|
||||||
return(result);
|
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);
|
unlink(f.tmp_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(request.session_id.length() > 0)
|
||||||
|
save_session_data(request.session_id, request.session);
|
||||||
|
|
||||||
request.time_end = microtime();
|
request.time_end = microtime();
|
||||||
printf("(r) %s\t%0.6fs\t%0.1fkB\n",
|
printf("(r) %s\t%0.6fs\t%0.1fkB\n",
|
||||||
request.params["REQUEST_URI"].c_str(),
|
request.params["REQUEST_URI"].c_str(),
|
||||||
@@ -90,6 +93,8 @@ FastCGIServer server;
|
|||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
srand(time());
|
||||||
|
|
||||||
// Set up our request handlers
|
// Set up our request handlers
|
||||||
server.request_handler(&handle_request);
|
server.request_handler(&handle_request);
|
||||||
server.data_handler(&handle_data);
|
server.data_handler(&handle_data);
|
||||||
@@ -110,6 +115,9 @@ int main(int argc, char** argv)
|
|||||||
dirname(server_state.config.COMPILER_SYS_PATH);
|
dirname(server_state.config.COMPILER_SYS_PATH);
|
||||||
basename(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(100);
|
||||||
//server.process();
|
//server.process();
|
||||||
server.process_forever();
|
server.process_forever();
|
||||||
|
|||||||
+8
-5
@@ -9,11 +9,7 @@ RENDER()
|
|||||||
<a href="index.uce">UCE Test</a>:
|
<a href="index.uce">UCE Test</a>:
|
||||||
Cookies
|
Cookies
|
||||||
</h1>
|
</h1>
|
||||||
<pre><?
|
Set
|
||||||
|
|
||||||
echo(var_dump(context->cookies));
|
|
||||||
|
|
||||||
?></pre>
|
|
||||||
<pre><?
|
<pre><?
|
||||||
|
|
||||||
set_cookie("test-cookie-1", "test-value-1", time() + 30*60);
|
set_cookie("test-cookie-1", "test-value-1", time() + 30*60);
|
||||||
@@ -21,6 +17,13 @@ RENDER()
|
|||||||
echo(var_dump(context->set_cookies));
|
echo(var_dump(context->set_cookies));
|
||||||
|
|
||||||
?></pre>
|
?></pre>
|
||||||
|
Get
|
||||||
|
<pre><?
|
||||||
|
|
||||||
|
echo(var_dump(context->cookies));
|
||||||
|
|
||||||
|
?></pre>
|
||||||
|
Params
|
||||||
<pre><?= var_dump(context->params) ?></pre>
|
<pre><?= var_dump(context->params) ?></pre>
|
||||||
</>
|
</>
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ RENDER()
|
|||||||
<li><a href="working-dir.uce">Working Directory</a></li>
|
<li><a href="working-dir.uce">Working Directory</a></li>
|
||||||
<li><a href="uri.uce">URI</a></li>
|
<li><a href="uri.uce">URI</a></li>
|
||||||
<li><a href="cookie.uce">Cookies</a></li>
|
<li><a href="cookie.uce">Cookies</a></li>
|
||||||
|
<li><a href="session.uce">Session</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<pre><?= var_dump(context->params) ?></pre>
|
<pre><?= var_dump(context->params) ?></pre>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -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>
|
||||||
|
</>
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@ Library
|
|||||||
- sha1 / md5 / meow
|
- sha1 / md5 / meow
|
||||||
- cache
|
- cache
|
||||||
- json_encode / json_decode
|
- json_encode / json_decode
|
||||||
- session
|
|
||||||
- curl
|
- curl
|
||||||
- random
|
- random
|
||||||
- pseudorandom
|
- pseudorandom
|
||||||
@@ -20,7 +19,7 @@ Bugs
|
|||||||
|
|
||||||
Framework
|
Framework
|
||||||
=================================
|
=================================
|
||||||
- DTree
|
- DTree<
|
||||||
- File Upload
|
- File Upload
|
||||||
- Proxy mode
|
- Proxy mode
|
||||||
- WebSockets
|
- WebSockets
|
||||||
|
|||||||
Reference in New Issue
Block a user