diff --git a/src/Core.h b/src/Core.h index 2cd57d1c7..48779a255 100644 --- a/src/Core.h +++ b/src/Core.h @@ -104,6 +104,11 @@ typedef struct TextureRec_ { float U1, V1, U2, V2; } TextureRec; #define CC_BUILD_X11 #define CC_BUILD_POSIX #endif +#ifdef __EMSCRIPTEN__ +#define CC_BUILD_WEB +#define CC_BUILD_SDL +#define CC_BUILD_POSIX +#endif #endif #ifdef CC_BUILD_D3D9 diff --git a/src/Http.c b/src/Http.c index f055c644d..b0537e9f6 100644 --- a/src/Http.c +++ b/src/Http.c @@ -740,6 +740,41 @@ static void Http_WorkerLoop(void) { } +/*########################################################################################################################* +*-------------------------------------------------------Http misc---------------------------------------------------------* +*#########################################################################################################################*/ +static bool Http_UrlDirect(uint8_t c) { + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') + || c == '-' || c == '_' || c == '.' || c == '~'; +} + +void Http_UrlEncode(String* dst, const uint8_t* data, int len) { + int i; + for (i = 0; i < len; i++) { + uint8_t c = data[i]; + + if (Http_UrlDirect(c)) { + String_Append(dst, c); + } else { + String_Append(dst, '%'); + String_AppendHex(dst, c); + } + } +} + +void Http_UrlEncodeUtf8(String* dst, const String* src) { + uint8_t data[4]; + Codepoint cp; + int i, len; + + for (i = 0; i < src->length; i++) { + cp = Convert_CP437ToUnicode(src->buffer[i]); + len = Convert_UnicodeToUtf8(cp, data); + Http_UrlEncode(dst, data, len); + } +} + + /*########################################################################################################################* *-----------------------------------------------------Http component------------------------------------------------------* *#########################################################################################################################*/ diff --git a/src/Http.h b/src/Http.h index 5a6dcdea1..60a1b5219 100644 --- a/src/Http.h +++ b/src/Http.h @@ -55,6 +55,11 @@ void Http_AsyncPostData(const String* url, bool priority, const String* id, cons /* Also sets the If-Modified-Since and If-None-Match headers. (if not NULL) */ void Http_AsyncGetDataEx(const String* url, bool priority, const String* id, TimeMS* lastModified, const String* etag); +/* Encodes data using % or URL encoding. */ +void Http_UrlEncode(String* dst, const uint8_t* data, int len); +/* Converts characters to UTF8, then calls Http_URlEncode on them. */ +void Http_UrlEncodeUtf8(String* dst, const String* src); + /* Attempts to retrieve a fully completed request. */ /* NOTE: You MUST also check Result/StatusCode, and check Size is > 0. */ /* (because a completed request may not have completed successfully) */ diff --git a/src/LWeb.c b/src/LWeb.c index 105f6b97b..d8ca6271f 100644 --- a/src/LWeb.c +++ b/src/LWeb.c @@ -286,10 +286,15 @@ static void SignInTask_Handle(uint8_t* data, uint32_t len) { Json_Handle(data, len, SignInTask_OnValue, NULL, NULL); } +static void SignInTask_Append(String* dst, const char* key, const String* value) { + String_AppendConst(dst, key); + Http_UrlEncodeUtf8(dst, value); +} + void SignInTask_Run(const String* user, const String* pass) { const static String id = String_FromConst("CC post login"); const static String url = String_FromConst("https://www.classicube.net/api/login"); - String tmp; char tmpBuffer[STRING_SIZE * 5]; + String tmp; char tmpBuffer[STRING_SIZE * 6]; if (SignInTask.Base.Working) return; LWebTask_Reset(&SignInTask.Base); @@ -297,9 +302,9 @@ void SignInTask_Run(const String* user, const String* pass) { SignInTask.Error.length = 0; String_InitArray(tmp, tmpBuffer); - /* TODO: URL ENCODE THIS.. */ - String_Format3(&tmp, "username=%s&password=%s&token=%s", - user, pass, &GetTokenTask.Token); + SignInTask_Append(&tmp, "username=", user); + SignInTask_Append(&tmp, "&password=", pass); + SignInTask_Append(&tmp, "&token=", &GetTokenTask.Token); SignInTask.Base.Identifier = id; Http_AsyncPostData(&url, false, &id, tmp.buffer, tmp.length); diff --git a/src/Launcher.c b/src/Launcher.c index 5babd5a1a..1d5582d6d 100644 --- a/src/Launcher.c +++ b/src/Launcher.c @@ -626,7 +626,6 @@ static void Launcher_ApplyUpdate(void) { res = Platform_MarkExecutable(&scriptPath); if (res) Logger_Warn(res, "making update script executable"); - /* TODO: (open -a Terminal ", '"' + path + '"'); on OSX */ res = Platform_StartProcess(&scriptName, &scriptArgs); if (res) { Logger_Warn(res, "starting update script"); return; } } diff --git a/src/Platform.c b/src/Platform.c index 711f9c884..be42920c5 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -107,6 +107,10 @@ const ReturnCode ReturnCode_SocketWouldBlock = EWOULDBLOCK; #include #include #endif +#ifdef CC_BUILD_WEB +#include +#include +#endif /*########################################################################################################################* @@ -368,6 +372,12 @@ uint64_t Stopwatch_Measure(void) { return mach_absolute_time(); } #endif +#ifdef CC_BUILD_WEB +uint64_t Stopwatch_Measure(void) { + /* time is a milliseconds double */ + return (uint64_t)(emscripten_get_now() * 1000); +} +#endif /*########################################################################################################################* @@ -1956,6 +1966,13 @@ ReturnCode Platform_GetExePath(String* path) { return 0; } #endif +#ifdef CC_BUILD_WEB +ReturnCode Platform_StartOpen(const String* args) { + char str[600]; + Platfrom_ConvetString(args, str); + EM_ASM_({ window.open(Pointer_stringify($0)); }, str); +} +#endif void* Platform_GetSymbolFrom(const char* filename, const char* name) { void* symbol; diff --git a/src/String.h b/src/String.h index 654c72484..cfdf879f3 100644 --- a/src/String.h +++ b/src/String.h @@ -166,6 +166,7 @@ bool Convert_TryUnicodeToCP437(Codepoint cp, char* c); /* Returns 0 if not enough input data to read the character. */ int Convert_Utf8ToUnicode(Codepoint* cp, const uint8_t* data, uint32_t len); /* Encodes a unicode character in UTF8, returning number of bytes written. */ +/* The number of bytes written is always either 1,2 or 3. */ int Convert_UnicodeToUtf8(Codepoint cp, uint8_t* data); /* Attempts to append all characters from UTF16 encoded data to the given string. */