Skip to content
Permalink
Browse files

http: Add a simple WebSocket server class.

  • Loading branch information...
unknownbrackets committed Apr 12, 2018
1 parent 65d072c commit b75b6801c80f31e0901d9b10f8f3916ddba032d1
@@ -893,6 +893,8 @@ add_library(native STATIC
ext/native/base/stringutil.h
ext/native/base/timeutil.cpp
ext/native/base/timeutil.h
ext/native/data/base64.cpp
ext/native/data/base64.h
ext/native/data/compression.cpp
ext/native/data/compression.h
ext/native/file/chunk_file.cpp
@@ -985,6 +987,8 @@ add_library(native STATIC
ext/native/net/sinks.h
ext/native/net/url.cpp
ext/native/net/url.h
ext/native/net/websocket_server.cpp
ext/native/net/websocket_server.h
ext/native/profiler/profiler.cpp
ext/native/profiler/profiler.h
ext/native/thin3d/thin3d.cpp
@@ -13,6 +13,7 @@ LOCAL_SRC_FILES :=\
base/timeutil.cpp \
base/colorutil.cpp \
base/stringutil.cpp \
data/base64.cpp \
data/compression.cpp \
ext/rg_etc1/rg_etc1.cpp \
ext/cityhash/city.cpp \
@@ -64,6 +65,7 @@ LOCAL_SRC_FILES :=\
net/resolve.cpp \
net/sinks.cpp \
net/url.cpp \
net/websocket_server.cpp \
profiler/profiler.cpp \
thread/executor.cpp \
thread/threadutil.cpp \
@@ -0,0 +1,35 @@
#include "data/base64.h"

// TODO: This is a simple but not very efficient implementation.
std::string Base64Encode(const uint8_t *p, size_t sz) {
const char digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

size_t unpaddedLength = (4 * sz + 3) / 3;
std::string result;
result.resize((unpaddedLength + 3) & ~3, '=');

for (size_t i = 0; i < unpaddedLength; ++i) {
// This is the index into the original string.
size_t pos = (i * 3) / 4;
int8_t off = 2 * ((i * 3) % 4);

int c = p[pos];
if (off > 2) {
c <<= 8;
off -= 8;

// Grab more bits from the next character.
if (pos + 1 < sz) {
c |= p[pos + 1];
}
}

// Since we take from the big end, off starts at 2 and goes down.
int8_t shift = 2 - off;

// Now take the bits at off and encode the character.
result[i] = digits[(c >> shift) & 0x3F];
}

return result;
}
@@ -0,0 +1,5 @@
#pragma once

#include <string>

std::string Base64Encode(const uint8_t *p, size_t sz);
@@ -202,6 +202,7 @@
<ClInclude Include="base\NativeApp.h" />
<ClInclude Include="base\stringutil.h" />
<ClInclude Include="base\timeutil.h" />
<ClInclude Include="data\base64.h" />
<ClInclude Include="data\compression.h" />
<ClInclude Include="data\listable.h" />
<ClInclude Include="ext\cityhash\city.h" />
@@ -238,6 +239,7 @@
<ClInclude Include="gfx_es2\draw_text_qt.h" />
<ClInclude Include="gfx_es2\draw_text_win.h" />
<ClInclude Include="json\json_reader.h" />
<ClInclude Include="net\websocket_server.h" />
<ClInclude Include="thin3d\d3d11_loader.h" />
<ClInclude Include="thin3d\DataFormat.h" />
<ClInclude Include="thin3d\DataFormatGL.h" />
@@ -317,6 +319,7 @@
</ClCompile>
<ClCompile Include="base\stringutil.cpp" />
<ClCompile Include="base\timeutil.cpp" />
<ClCompile Include="data\base64.cpp" />
<ClCompile Include="data\compression.cpp" />
<ClCompile Include="ext\cityhash\city.cpp">
<InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">AnySuitable</InlineFunctionExpansion>
@@ -700,6 +703,7 @@
<ClCompile Include="gfx_es2\draw_text_win.cpp" />
<ClCompile Include="json\json_reader.cpp" />
<ClCompile Include="math\dataconv.cpp" />
<ClCompile Include="net\websocket_server.cpp" />
<ClCompile Include="thin3d\d3d11_loader.cpp" />
<ClCompile Include="thin3d\DataFormatGL.cpp" />
<ClCompile Include="thin3d\GLQueueRunner.cpp" />
@@ -341,6 +341,12 @@
<ClInclude Include="json\json_reader.h">
<Filter>json</Filter>
</ClInclude>
<ClInclude Include="net\websocket_server.h">
<Filter>net</Filter>
</ClInclude>
<ClInclude Include="data\base64.h">
<Filter>data</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="gfx\gl_debug_log.cpp">
@@ -814,6 +820,12 @@
<ClCompile Include="json\json_reader.cpp">
<Filter>json</Filter>
</ClCompile>
<ClCompile Include="net\websocket_server.cpp">
<Filter>net</Filter>
</ClCompile>
<ClCompile Include="data\base64.cpp">
<Filter>data</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Filter Include="gfx">
@@ -87,8 +87,10 @@ void Request::WriteHttpResponseHeader(int status, int64_t size, const char *mime
net::OutputSink *buffer = Out();
buffer->Printf("HTTP/1.0 %03d %s\r\n", status, statusStr);
buffer->Push("Server: PPSSPPServer v0.1\r\n");
buffer->Printf("Content-Type: %s\r\n", mimeType ? mimeType : DEFAULT_MIME_TYPE);
buffer->Push("Connection: close\r\n");
if (!mimeType || strcmp(mimeType, "websocket") != 0) {
buffer->Printf("Content-Type: %s\r\n", mimeType ? mimeType : DEFAULT_MIME_TYPE);
buffer->Push("Connection: close\r\n");
}
if (size >= 0) {
buffer->Printf("Content-Length: %llu\r\n", size);
}
Oops, something went wrong.

0 comments on commit b75b680

Please sign in to comment.
You can’t perform that action at this time.