This commit is contained in:
Udo 2021-10-30 01:51:46 +00:00
parent fa432a0482
commit af3f2a1f96
20 changed files with 772 additions and 296 deletions

Binary file not shown.

View File

@ -17,6 +17,8 @@ SO_FN="$5"
mkdir -p "$DEST_DIR" > /dev/null 2>&1 mkdir -p "$DEST_DIR" > /dev/null 2>&1
export CPLUS_INCLUDE_PATH="${CPLUS_INCLUDE_PATH:+${CPLUS_INCLUDE_PATH}:}$SRC_DIR"
BUILDMODE="debug" BUILDMODE="debug"
OPT_FLAG="O0" OPT_FLAG="O0"

View File

@ -254,7 +254,11 @@ SharedUnit* get_shared_unit(string file_name)
void invoke(string file_name) void invoke(string file_name)
{ {
printf("(i) invoke(%s)\n", file_name.c_str()); if(file_name[0] != '/')
{
file_name = expand_path(file_name);
}
//printf("(i) invoke(%s)\n", file_name.c_str());
auto su = get_shared_unit(file_name); auto su = get_shared_unit(file_name);
if(!su) if(!su)
{ {

View File

@ -10,6 +10,18 @@ string var_dump(StringMap map, string prefix = "", string postfix = "\n")
return(result); return(result);
} }
string var_dump(StringList map, string prefix = "", string postfix = "\n")
{
string result = "";
for (u32 i = 0; i < map.size(); i++)
{
result.append(prefix + map[i] + postfix);
}
return(result);
}
u8 char_to_u8(char input) u8 char_to_u8(char input)
{ {
if(input >= '0' && input <= '9') if(input >= '0' && input <= '9')
@ -26,6 +38,15 @@ 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)
{
static const char* digits = "0123456789ABCDEF";
string rc(hex_len,'0');
for (size_t i=0, j=(hex_len-1)*4 ; i<hex_len; ++i,j-=4)
rc[i] = digits[(w>>j) & 0x0f];
return(rc);
}
string trim(string raw) string trim(string raw)
{ {
u32 len = raw.length(); u32 len = raw.length();
@ -47,22 +68,24 @@ StringList explode(string str, string delim = "\n")
StringList result; StringList result;
int start = 0; int start = 0;
int end = str.find(delim); int end = str.find(delim);
while (end != -1) while (end != string::npos)
{ {
result.push_back(str.substr(start, end - start)); result.push_back(str.substr(start, end - start));
start = end + delim.size(); start = end + delim.size();
end = str.find(delim, start); end = str.find(delim, start);
} }
result.push_back(str.substr(start, end - start)); result.push_back(str.substr(start, end - start));
return(result);
} }
string implode(StringList l, string delim = "\n") string implode(StringList l, string delim = "\n")
{ {
string result; string result;
for(string &s : l) for(u32 i = 0; i < l.size(); i++)
{ {
if(result.length() > 0) string s = l[i];
result.append(delim+s); if(i > 0)
result.append(delim);
result.append(s); result.append(s);
} }
return(result); return(result);

View File

@ -1,12 +0,0 @@
struct ServerSettings {
string BIN_DIRECTORY = "work";
string COMPILE_SCRIPT = "scripts/compile";
string LIT_ESC = "3d5b5_1";
string CONTENT_TYPE = "text/html; charset=utf-8";
string SOCKET_PATH = "/run/uce.sock";
string TMP_UPLOAD = "/tmp";
string COMPILER_SYS_PATH = "";
u32 LISTEN_PORT = 9993;
};

View File

@ -49,12 +49,21 @@ $ U+0024 Dollar sign Parameter expansion
string basename(string fn) string basename(string fn)
{ {
return(trim(shell_exec("basename "+shell_escape(fn)))); string result;
while(fn.length() > 0)
result = nibble("/", fn);
//printf("basename(%s) %s\n", fn.c_str(), result.c_str());
return(result);
} }
string dirname(string fn) string dirname(string fn)
{ {
return(trim(shell_exec("dirname "+shell_escape(fn)))); string result;
auto seg = explode(fn, "/");
seg.pop_back();
result = implode(seg, "/");
//printf("dirname(%s) %s seg#%i\n", fn.c_str(), result.c_str(), seg.size());
return(result);
} }
bool file_exists(string path) bool file_exists(string path)
@ -109,7 +118,7 @@ time_t file_mtime(string file_name)
struct stat info; struct stat info;
if (stat(file_name.c_str(), &info) != 0) if (stat(file_name.c_str(), &info) != 0)
{ {
printf("file_mtime(%s) error\n", file_name.c_str()); return(0);
} }
else else
{ {
@ -121,3 +130,15 @@ void unlink(string file_name)
{ {
remove(file_name.c_str()); remove(file_name.c_str());
} }
string expand_path(string path)
{
string result;
char buf[PATH_MAX];
char *res = realpath(path.c_str(), buf);
if(res)
{
result.append(res);
}
return(result);
}

View File

@ -13,8 +13,10 @@ string date(string format = "", u64 timestamp = 0)
{ {
string ts; string ts;
string fmt; string fmt;
if(timestamp > 0) ts = string("-d '@")+to_string(timestamp)+"'"; if(timestamp > 0)
if(format != "") fmt = string("+'"+format+"'"); ts = string("-d '@")+to_string(timestamp)+"'";
if(format != "")
fmt = string("+'"+format+"'");
return(trim(shell_exec("date "+ts+" "+fmt))); return(trim(shell_exec("date "+ts+" "+fmt)));
} }
@ -22,12 +24,16 @@ string gmdate(string format = "", u64 timestamp = 0)
{ {
string ts; string ts;
string fmt; string fmt;
if(timestamp > 0) ts = string("'@")+to_string(timestamp)+"'"; if(timestamp > 0)
if(format != "") fmt = string("+'"+format+"'"); ts = string("-d '@")+to_string(timestamp)+"'";
if(format == "RFC1123")
format = "%a, %d %b %Y %T GMT";
if(format != "")
fmt = string("+'"+format+"'");
return(trim(shell_exec("date -u "+ts+" "+fmt))); return(trim(shell_exec("date -u "+ts+" "+fmt)));
} }
u64 parse_time(string time_string) u64 parse_time(string time_string)
{ {
return(int_val(trim(shell_exec("date -u -d '"+time_string+"' +'%s'")))); return(int_val(trim(shell_exec("date -u -d '"+time_string+"' +'%s'"))));
} }

View File

@ -2,7 +2,6 @@
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include <string> #include <string>
//#include <unordered_map>
#include <map> #include <map>
#include <filesystem> #include <filesystem>
#include <ctype.h> #include <ctype.h>
@ -11,6 +10,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <ctime> #include <ctime>
#include <dlfcn.h> #include <dlfcn.h>
#include <limits.h>
typedef std::string string; typedef std::string string;
@ -36,17 +36,24 @@ typedef std::vector<string> StringList;
typedef void (*invoke_handler)(string file_name); typedef void (*invoke_handler)(string file_name);
struct UploadedFile {
string file_name;
string tmp_name;
u32 size;
};
struct Request; struct Request;
typedef void (*call_handler)(); typedef void (*call_handler)();
typedef void (*request_handler)(Request* request); typedef void (*request_handler)(Request* request);
struct ServerSettings {
string BIN_DIRECTORY = "work";
string COMPILE_SCRIPT = "scripts/compile";
string LIT_ESC = "3d5b5_1";
string CONTENT_TYPE = "text/html; charset=utf-8";
string SOCKET_PATH = "/run/uce.sock";
string TMP_UPLOAD = "/tmp";
string COMPILER_SYS_PATH = "";
u32 LISTEN_PORT = 9993;
};
struct SharedUnit { struct SharedUnit {
string file_name; string file_name;
@ -78,3 +85,52 @@ struct SharedUnit {
#include "dtree.h" #include "dtree.h"
struct UploadedFile {
string file_name;
string tmp_name;
u32 size;
};
struct ServerState {
std::map<string, SharedUnit*> units;
ServerSettings config;
} server_state;
struct Request {
ServerState* server_state;
StringMap params;
StringMap get;
StringMap post;
StringMap cookies;
std::vector<UploadedFile> uploaded_files;
StringMap header;
StringList set_cookies;
std::string in;
std::string out;
std::string err;
f64 time_init;
f64 time_start;
f64 time_end;
void print(string s)
{
out.append(s);
}
};
struct URI {
StringMap query;
StringMap parts;
};
typedef Request FastCGIRequest;

View File

@ -1,44 +1,10 @@
#include "types.h" #include "types.h"
#include "settings.h"
#include "datastructures.h" #include "datastructures.h"
#include "sys.h" #include "sys.h"
#include "time.h" #include "time.h"
struct ServerState {
std::map<string, SharedUnit*> units;
ServerSettings config;
} server_state;
struct Request {
ServerState* server_state;
StringMap params;
StringMap get;
StringMap post;
std::vector<UploadedFile> uploaded_files;
StringMap header;
std::string in;
std::string out;
std::string err;
f64 time_init;
f64 time_start;
f64 time_end;
void print(string s)
{
out.append(s);
}
};
typedef Request FastCGIRequest;
Request* context; Request* context;
#include "uri.h" #include "uri.h"

View File

@ -1,221 +1,4 @@
struct URI {
StringMap query;
StringMap parts;
static URI parse(string uri_string)
{
URI result;
u8 state = 0;
string current = "";
char expect = 0;
result.parts["raw"] = uri_string;
string part_names[] = {
"scheme",
"host",
"port",
"path",
"query",
"fragment",
};
if(uri_string[0] == '/')
state = 3;
for (char &c: uri_string)
{
bool append_it = true;
if(expect && expect != c)
{
result.parts["error"] = string("\'") + c + string("\' expected");
result.parts["error_parsing"] = current;
result.parts["error_part"] = part_names[state];
return(result);
}
expect = 0;
switch(state)
{
case(0): // scheme
if(c == ':')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 1;
}
break;
case(1): // host name
if(c == '/')
{
if(current == "")
{
append_it = false;
break;
}
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 3;
expect = '/';
}
else if(c == ':')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 2;
}
break;
case(2): // port
if(c == '/')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 3;
expect = '/';
}
break;
case(3): // path
if(c == '/' && current == "")
{
append_it = false;
break;
}
if(c == '?')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 4;
}
break;
case(4): // query
if(c == '#')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 5;
}
break;
}
if(append_it)
current.append(1, c);
}
result.parts[part_names[state]] = current;
result.query = URI::parse_query(result.parts["query"]);
return(result);
}
static StringMap parse_query(string q)
{
StringMap result;
if(q.length() == 0)
return(result);
bool is_key = true;
string key = "";
string value = "";
for (char &c: q)
{
if(c == '=')
{
is_key = !is_key;
}
else if(c == '&')
{
result[URI::decode(key)] = URI::decode(value);
key = "";
value = "";
is_key = true;
}
else if(is_key)
{
key.append(1, c);
}
else
{
value.append(1, c);
}
}
result[URI::decode(key)] = URI::decode(value);
return(result);
}
static StringMap parse_multipart(string q, string boundary, std::vector<UploadedFile>& uploaded_files)
{
StringMap result;
auto i = boundary.length();
while(i < q.length())
{
auto end_pos = q.find(boundary, i);
if(end_pos != string::npos)
{
string field = q.substr(i, end_pos - i);
nibble(":", field);
string ftype = trim(nibble(";", field));
if(ftype == "form-data")
{
nibble("=\"", field);
string field_name = nibble("\"", field);
result[field_name] = field.substr(4, field.length()-6);
}
else if(ftype == "attachment")
{
nibble("=\"", field);
UploadedFile f;
f.tmp_name = context->server_state->config.TMP_UPLOAD + to_string(rand());
f.file_name = nibble("\"", field);
string bin = field.substr(4, field.length()-6);
f.size = bin.length();
file_put_contents(f.tmp_name, bin);
uploaded_files.push_back(f);
}
}
else
{
// we're done
end_pos = q.length();
}
i = end_pos + boundary.length();
}
return(result);
}
static string decode(string q)
{
string result;
for(u32 i = 0; i < q.length(); i++)
{
char c = q[i];
if(c == '%' && q[i+1] != '%')
{
result.append(1, hex_to_u8(q.substr(i+1, 2)));
i += 2;
}
else
{
result.append(1, c);
}
}
return(result);
}
};
string var_dump(URI uri, string prefix = "", string postfix = "\n") string var_dump(URI uri, string prefix = "", string postfix = "\n")
{ {
@ -226,3 +9,257 @@ string var_dump(URI uri, string prefix = "", string postfix = "\n")
var_dump(uri.query, prefix+" ", postfix) var_dump(uri.query, prefix+" ", postfix)
); );
} }
string uri_decode(string q)
{
string result;
for(u32 i = 0; i < q.length(); i++)
{
char c = q[i];
if(c == '%' && q[i+1] != '%')
{
result.append(1, hex_to_u8(q.substr(i+1, 2)));
i += 2;
}
else
{
result.append(1, c);
}
}
return(result);
}
string uri_encode(string q)
{
string result;
for(u32 i = 0; i < q.length(); i++)
{
char c = q[i];
if(isalnum(c) || c == '~' || c == '.' || c == '_' || c == '-')
result.append(1, c);
else
{
result.append(1, '%');
result.append(to_hex(c));
}
}
return(result);
}
StringMap parse_query(string q)
{
StringMap result;
if(q.length() == 0)
return(result);
bool is_key = true;
string key = "";
string value = "";
for (char &c: q)
{
if(c == '=')
{
is_key = !is_key;
}
else if(c == '&')
{
result[uri_decode(key)] = uri_decode(value);
key = "";
value = "";
is_key = true;
}
else if(is_key)
{
key.append(1, c);
}
else
{
value.append(1, c);
}
}
result[uri_decode(key)] = uri_decode(value);
return(result);
}
StringMap parse_multipart(string q, string boundary, std::vector<UploadedFile>& uploaded_files)
{
StringMap result;
auto i = boundary.length();
while(i < q.length())
{
auto end_pos = q.find(boundary, i);
if(end_pos != string::npos)
{
string field = q.substr(i, end_pos - i);
nibble(":", field);
string ftype = trim(nibble(";", field));
if(ftype == "form-data")
{
nibble("=\"", field);
string field_name = nibble("\"", field);
result[field_name] = field.substr(4, field.length()-6);
}
else if(ftype == "attachment")
{
nibble("=\"", field);
UploadedFile f;
f.tmp_name = context->server_state->config.TMP_UPLOAD + to_string(rand());
f.file_name = nibble("\"", field);
string bin = field.substr(4, field.length()-6);
f.size = bin.length();
file_put_contents(f.tmp_name, bin);
uploaded_files.push_back(f);
}
}
else
{
// we're done
end_pos = q.length();
}
i = end_pos + boundary.length();
}
return(result);
}
URI parse_uri(string uri_string)
{
URI result;
u8 state = 0;
string current = "";
char expect = 0;
result.parts["raw"] = uri_string;
string part_names[] = {
"scheme",
"host",
"port",
"path",
"query",
"fragment",
};
if(uri_string[0] == '/')
state = 3;
for (char &c: uri_string)
{
bool append_it = true;
if(expect && expect != c)
{
result.parts["error"] = string("\'") + c + string("\' expected");
result.parts["error_parsing"] = current;
result.parts["error_part"] = part_names[state];
return(result);
}
expect = 0;
switch(state)
{
case(0): // scheme
if(c == ':')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 1;
}
break;
case(1): // host name
if(c == '/')
{
if(current == "")
{
append_it = false;
break;
}
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 3;
expect = '/';
}
else if(c == ':')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 2;
}
break;
case(2): // port
if(c == '/')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 3;
expect = '/';
}
break;
case(3): // path
if(c == '/' && current == "")
{
append_it = false;
break;
}
if(c == '?')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 4;
}
break;
case(4): // query
if(c == '#')
{
result.parts[part_names[state]] = current;
append_it = false;
current = "";
state = 5;
}
break;
}
if(append_it)
current.append(1, c);
}
result.parts[part_names[state]] = current;
result.query = parse_query(result.parts["query"]);
return(result);
}
void set_cookie(
string name, string value = "",
u64 expires = 0, string path = "/", string domain = "",
bool secure = false, bool http_only = true)
{
string cookie = "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);
}
StringMap parse_cookies(string cookie_string)
{
StringMap result;
while(cookie_string.length() > 0)
{
string key = trim(nibble("=", cookie_string));
string value = nibble(";", cookie_string);
result[key] = value;
}
return(result);
}

View File

@ -43,7 +43,10 @@ int handle_complete(FastCGIRequest& request) {
request.time_start = microtime(); request.time_start = microtime();
request.header["Content-Type"] = context->server_state->config.CONTENT_TYPE; request.header["Content-Type"] = context->server_state->config.CONTENT_TYPE;
request.get = URI::parse_query(request.params["QUERY_STRING"]); request.get = parse_query(request.params["QUERY_STRING"]);
if(request.params["HTTP_COOKIE"].length() > 0)
request.cookies = parse_cookies(request.params["HTTP_COOKIE"]);
string ct_info = request.params["CONTENT_TYPE"]; string ct_info = request.params["CONTENT_TYPE"];
string ct_type = nibble(";", ct_info); string ct_type = nibble(";", ct_info);
@ -53,17 +56,20 @@ int handle_complete(FastCGIRequest& request) {
if(ct_type == "multipart/form-data") if(ct_type == "multipart/form-data")
{ {
nibble("boundary=", ct_info); nibble("boundary=", ct_info);
request.post = URI::parse_multipart(request.in, string("--")+ct_info, request.uploaded_files); request.post = parse_multipart(request.in, string("--")+ct_info, request.uploaded_files);
} }
else else
{ {
request.post = URI::parse_query(request.in); request.post = parse_query(request.in);
} }
} }
invoke(request.params["SCRIPT_FILENAME"]); invoke(request.params["SCRIPT_FILENAME"]);
string headers = var_dump(request.header, "", "\r\n") + "\r\n"; string headers =
var_dump(request.header, "", "\r\n") +
var_dump(request.set_cookies, "", "\r\n") +
"\r\n";
request.out = headers + request.out; request.out = headers + request.out;
for( auto &f : request.uploaded_files) for( auto &f : request.uploaded_files)
@ -101,6 +107,9 @@ int main(int argc, char** argv)
server.listen(server_state.config.SOCKET_PATH); server.listen(server_state.config.SOCKET_PATH);
chmod(server_state.config.SOCKET_PATH.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); chmod(server_state.config.SOCKET_PATH.c_str(), S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
dirname(server_state.config.COMPILER_SYS_PATH);
basename(server_state.config.COMPILER_SYS_PATH);
//server.process(100); //server.process(100);
//server.process(); //server.process();
server.process_forever(); server.process_forever();

27
test/cookie.uce Normal file
View File

@ -0,0 +1,27 @@
RENDER()
{
<>
<link rel="stylesheet" href='style.css'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Cookies
</h1>
<pre><?
echo(var_dump(context->cookies));
?></pre>
<pre><?
set_cookie("test-cookie-1", "test-value-1", time() + 30*60);
set_cookie("test-cookie-2", "test-value-2", time() + 30*60*24);
echo(var_dump(context->set_cookies));
?></pre>
<pre><?= var_dump(context->params) ?></pre>
</>
}

4
test/inc-test.cpp Normal file
View File

@ -0,0 +1,4 @@
void test_output()
{
echo("hello from include!");
}

View File

@ -3,7 +3,7 @@
RENDER() RENDER()
{ {
<html> <>
<link rel="stylesheet" href='style.css'></link> <link rel="stylesheet" href='style.css'></link>
<h1> <h1>
<a href="index.uce">UCE Test</a>: <a href="index.uce">UCE Test</a>:
@ -14,8 +14,10 @@ RENDER()
<li><a href="post.uce">Form Post</a></li> <li><a href="post.uce">Form Post</a></li>
<li><a href="post-multipart.uce">Form Multipart Post</a></li> <li><a href="post-multipart.uce">Form Multipart Post</a></li>
<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="cookie.uce">Cookies</a></li>
</ul> </ul>
<pre><?= var_dump(context->params) ?></pre> <pre><?= var_dump(context->params) ?></pre>
</html> </>
} }

28
test/uri.uce Normal file
View File

@ -0,0 +1,28 @@
RENDER()
{
<>
<link rel="stylesheet" href='style.css'></link>
<h1>
<a href="index.uce">UCE Test</a>:
URI
</h1>
<form action="?" method="post">
<div>
<label>uri_encode</label>
<input type="text" name="uri_encode" value="<?= context->post["uri_encode"] ?>"/>
<br/>
Encode: <?= uri_encode(context->post["uri_encode"]) ?>
<br/>
Decode: <?= uri_decode(uri_encode(context->post["uri_encode"])) ?>
</div>
<div>
<input type="submit" value="Submit Form"/>
</div>
</form>
<pre><?= var_dump(context->params) ?></pre>
</>
}

View File

@ -5,7 +5,6 @@ Library
- sha1 / md5 / meow - sha1 / md5 / meow
- cache - cache
- json_encode / json_decode - json_encode / json_decode
- cookies
- session - session
- curl - curl
- random - random
@ -15,9 +14,8 @@ Library
Bugs Bugs
================================= =================================
- shell_escape() - shell_escape()
- include path - we crash on illegal paths
- preprocessor - you should not be able to invoke() outside of DOCUMENT_ROOT
- nested tags
Framework Framework

View File

@ -0,0 +1,191 @@
#include "/Code/uce.openfu.com/uce/src/lib/uce_gen.h"
void show_stuff()
{
//context->header["Content-Type"] = "text/plain";
//echo(to_string(time())+"\n");
context->print(R"(<html>
Mwahahaaha )");echo(html_escape( time() )); context->print(R"(
<div>hello world: )");echo(html_escape( context->params["HTTP_HOST"] )); context->print(R"(</div>
<pre>
Display the date using any or all of the following elements:
%a: abbreviated day name (i.e. mon, tue, wed)
%A: full day name (i.e. Monday, Tuesday, Wednesday)
%b or %h: abbreviated month name (i.e. jan, feb, mar)
%B: full month name (January, February, March)
%c: locales date and time (full date and time)
%C: century - displays the first two numbers of the year (i.e 19 for 1999 and 20 for 2020)
%d: day of month (i.e. 01, 02, 03)
%D: same as M/D/Y (i.e. 04/20/16)
%e: day of month padded (i.e. ' 1', ' 2')
%F: full date, same as yyyy-mm-dd
%H: hour (00, 01, 02, 21, 22, 23)
%I: hour (1,2,3,10,11,12)
%j: day of year (i.e. 243)
%k: hour padded (i.e. '1' becomes ' 1'
%l: hour padded (12 hour clock)
%m: month number (1,2,3)
%M: minute (1,2,3,57,58,59)
%n: new line
%N: nanoseconds
%p: AM or PM
%P: like %p but lowercase (ironically)
%r: locales 12 hour clock time
%R: 24 hour version of hour and minute
%s: seconds since 1970-01-01 00:00:00
%S: second (01,02,03, 57, 58, 59)
%t: a tab
%T: time same as %H:%M:%S
%u: day of week (1 is Monday, 2 is Tuesday etc)
%U: week number of year (assuming Sunday as first day of the week)
%V: ISO week number with Monday as the first day of the week
%w: day of week (0 is Sunday)
%W: week number of the year with Monday as the first day of the week
%x: locales date representation (12/31/2015)
%X: locales time representation (14:44:44)
%y: last two digits of year
%Y: year
%z: numeric time zone (i.e. -0400)
%:z: numeric time zone as follows (i.e. -04:00)
%::z: numeric time zone as follows (i.e. -04:00:00)
%Z: alphabetic time zone abbreviation (GMT)
-: a single hyphen prevents zero padding
_: a single underscore pads with spaces
0: pads with zeroes
^: use uppercase if possible
#: use opposite case if possible
To display just the time use the following:
date +%T
Alternatively, use the following:
date +%H:%M:%S
Attach the date, as well, using the command:
date +%d/%m/%Y%t%H:%M:%S
Alternatively, use the follow (since %T is equivalent to %H:%M:%S):
date +$d/%m/%Y%t%T
The : and / characters are optional and can be whatever you want. For example:
date +%dc%mc%Y
outputs: 24c09c2020, if you wanted to use 'c' as a delimiter for some reason.
Use any combination of the above switches after the plus symbol to output the date as you so wish. If you want to add spaces you can use quotes around the date.
date +'%d/%m/%Y %H:%M:%S'
How to Show the UTC Date
View the UTC date for your computer using the following command:
date -u
If you are in the UK you will notice that instead of showing "18:58:20" as the time it will show "17:58:20" as the time.
How to Show the RFC Date
View the RFC date for your computer using the following command:
date --rfc-2822
This displays the date in the following format:
Wed, 20 Apr 2018 19:56:52 +0100
This flag is useful as it shows that you are an hour ahead of GMT.
Some Useful Date Commands
Do you want to know the date next Monday? Try this:
date -d "next Monday"
At the point of writing this returns "Mon 25 Apr 00:00:00 BST 2016"
The -d basically prints a date in the future or the past. So, you can use "next Monday" or "last Friday".
Using the same command you can find out which day of the week your birthday or Christmas falls upon.
date -d 12/25/2016
The result is Sun Dec 25.
Summary
It is worth checking out the manual page for the date command using the following command:
man date
Was this page helpful?
More from Lifewire
Businessman checking the time on his watch
How to Understand the Date and Time in Email Headers
Calendar next to an Apple keyboard
How to Change the Date and Time on a Mac Manually
Turning on automatic date and time settings in Windows 10.
Change the Date and Time Zone on Your Windows Laptop
People comparing the calendars on their iPhones
How to Change Date on iPhone
Woman walking with Black Friday shopping bag
What Is Black Friday?
Clock On White Wall
Find the Sent Timestamp on Gmail Messages
Homescreen with clock on Android
How to Change the Time on Android
Person taking a photo of a flower with a smartphone
How to Adjust the Date, Time, and Location of Photos in iOS 15
Dark office with many computers, one lit up
Understanding the Linux Command: Ar
A human hand pressing an old-fashioned alarm clock
Learn the Linux Command 'at'
Close-Up Of Thumbtack On Calendar Date
Using the DATE Function in Google Sheets
DATE function in Excel
How to Use the Excel DATE Function
Tux the penguin is the official Linux mascot.
Delete Files Using the Linux Command Line
Close-Up Of Clock Against Calendar
Serial Number and Serial Date in Excel
Person running Linux sleep command for 20 seconds on a laptop
How to Use the Linux Sleep Command to Pause a BASH Script
Cropped Hand Of Person Using Laptop By Alarm Clock At Table
Excel's Volatile NOW Function for the Date and Time
Lifewire
Tech for Humans
Follow Us
Subscribe to our newsletter and get techs top stories in 30 seconds.
Email Address
enter email
SUBMIT
News
Best Products
Mobile Phones
Computers
About Us
Advertise
Privacy Policy
Cookie Policy
Careers
Editorial Guidelines
Contact
Terms of Use
EU Privacy
California Privacy Notice
Lifewire is part of the Dotdash publishing family.
</pre>
</html>)");
}
RENDER()
{
context->print(R"(<html>
<link rel="stylesheet" href='style.css?v=1'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Index
</h1>
)"); show_stuff(); context->print(R"(
<pre>)");echo(html_escape( var_dump(context->params) )); context->print(R"(</pre>
</html>)");
}

View File

@ -0,0 +1,22 @@
#include "/Code/uce.openfu.com/uce/src/lib/uce_gen.h"
RENDER()
{
context->print(R"(<html>
<link rel="stylesheet" href='style.css'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Index
</h1>
<ul>
<li><a href="hello.uce">Hello</a></li>
<li><a href="post.uce">Form Post</a></li>
<li><a href="post-multipart.uce">Form Multipart Post</a></li>
<li><a href="working-dir.uce">Working Directory</a></li>
</ul>
<pre>)");echo(html_escape( var_dump(context->params) )); context->print(R"(</pre>
</html>)");
}

View File

@ -0,0 +1,48 @@
#include "/Code/uce.openfu.com/uce/src/lib/uce_gen.h"
void show_form()
{
context->print(R"(<form action="?" method="post" enctype="multipart/form-data">
<div>
<label>Some text:</label>
<input type="text" name="fühld" value=")");echo(html_escape( context->post["fühld"] )); context->print(R"("/>
</div>
<div>
<label>Some multiline text:</label>
<textarea name="field2">)");echo(html_escape( context->post["field2"] )); context->print(R"(</textarea>
</div>
<div>
<input type="submit" value="Submit Form"/>
</div>
</form>)");
}
RENDER()
{
context->print(R"(
<link rel="stylesheet" href='style.css?v=1'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Multipart-Encoded Form POST
</h1>
)"); show_form(); context->print(R"(
<div>
</div>
<label>Parsed POST fields</label>
<pre>)");echo(html_escape( var_dump(context->post) )); context->print(R"(</pre>
<label>Raw POST content</label>
<pre>)");echo(html_escape( context->in )); context->print(R"(</pre>
<label>CGI Params</label>
<pre>)");echo(html_escape( var_dump(context->params) )); context->print(R"(</pre>
)");
}

View File

@ -0,0 +1,44 @@
#include "/Code/uce.openfu.com/uce/src/lib/uce_gen.h"
void show_form()
{
context->print(R"(<form action="?" method="post">
<div>
<label>Some text:</label>
<input type="text" name="field" value=")");echo(html_escape( context->post["field"] )); context->print(R"("/>
</div>
<div>
<label>Some multiline text:</label>
<textarea name="field2">)");echo(html_escape( context->post["field2"] )); context->print(R"(</textarea>
</div>
<div>
<input type="submit" value="Submit Form"/>
</div>
</form>)");
}
RENDER()
{
context->print(R"(<html>
<link rel="stylesheet" href='style.css?v=1'></link>
<h1>
<a href="index.uce">UCE Test</a>:
Form POST
</h1>
)"); show_form(); context->print(R"(
<label>Parsed POST fields</label>
<pre>)");echo(html_escape( var_dump(context->post) )); context->print(R"(</pre>
<label>Raw POST content</label>
<pre>)");echo(html_escape( context->in )); context->print(R"(</pre>
<label>CGI Params</label>
<pre>)");echo(html_escape( var_dump(context->params) )); context->print(R"(</pre>
</html>)");
}