Skip to content

Commit 8f188ba

Browse files
committed
Http: Fix issues introduced when removing dependency from String
1 parent d46a112 commit 8f188ba

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

Libraries/Http/HttpServer.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../Memory/String.h"
77
#include "../Socket/Socket.h"
88
#include "Internal/HttpStringAppend.h"
9+
#include <stdio.h>
910

1011
// HttpRequest
1112
bool SC::HttpRequest::find(HttpParser::Token token, StringSpan& res) const
@@ -58,11 +59,20 @@ SC::Result SC::HttpResponse::end(Span<const char> span)
5859
GrowableBuffer<decltype(outputBuffer)> gb = {outputBuffer};
5960

6061
HttpStringAppend& sb = static_cast<HttpStringAppend&>(static_cast<IGrowableBuffer&>(gb));
61-
SC_TRY(sb.append("Content-Length: {}\r\n\r\n", span.sizeInBytes()));
62+
SC_TRY(sb.append("Content-Length:"));
63+
char bufferSize[32];
64+
snprintf(bufferSize, sizeof(bufferSize), "%zu", span.sizeInBytes());
65+
StringSpan ss = {{bufferSize, strlen(bufferSize)}, false, StringEncoding::Ascii};
66+
SC_TRY(sb.append(ss));
67+
SC_TRY(sb.append("\r\n\r\n"));
6268
}
6369
SC_TRY(not outputBuffer.isEmpty());
64-
SC_TRY(outputBuffer.resizeWithoutInitializing(outputBuffer.size() - 1)); // pop null terminator
6570
SC_TRY(outputBuffer.append(span));
71+
return end();
72+
}
73+
74+
SC::Result SC::HttpResponse::end()
75+
{
6676
responseEnded = true;
6777
return Result(true);
6878
}

Libraries/Http/HttpServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct SC::HttpResponse
8787
/// @brief Finalizes response appending some data
8888
/// @warning The SC::HttpResponse / SC::HttpRequest pair will be invalidated on next SC::AsyncEventLoop run
8989
Result end(Span<const char> data);
90+
Result end();
9091

9192
private:
9293
friend struct HttpServer;

Libraries/Http/HttpWebServer.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ SC::Result SC::HttpWebServer::Internal::readFile(StringSpan directory, HttpReque
7070
SC_TRY(Internal::writeGMTHeaderTime("Last-Modified", response, fileStat.modifiedTime.milliseconds));
7171
SC_TRY(response.end(data.toSpanConst()));
7272
}
73+
else
74+
{
75+
response.startResponse(404);
76+
SC_TRY(response.addHeader("Connection", "Closed"));
77+
SC_TRY(response.addHeader("Server", "SC"));
78+
response.end();
79+
}
7380
return Result(true);
7481
}
7582

@@ -95,6 +102,30 @@ SC::StringSpan SC::HttpWebServer::Internal::getContentType(const StringSpan exte
95102
{
96103
return "image/svg+xml";
97104
}
105+
if (extension == "js")
106+
{
107+
return "application/javascript";
108+
}
109+
if (extension == "json")
110+
{
111+
return "application/json";
112+
}
113+
if (extension == "xml")
114+
{
115+
return "application/xml";
116+
}
117+
if (extension == "pdf")
118+
{
119+
return "application/pdf";
120+
}
121+
if (extension == "ico")
122+
{
123+
return "image/x-icon";
124+
}
125+
if (extension == "txt")
126+
{
127+
return "text/plain";
128+
}
98129
return "text/html";
99130
}
100131

Tests/Libraries/Containers/ArrayTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ struct SC::ArrayTest : public SC::TestCase
165165
auto myArr4 = move(myArr1);
166166
SC_TEST_EXPECT(myArr4.size() == myArr3.size());
167167
SC_TEST_EXPECT(memcmp(myArr4.data(), myArr3.data(), myArr3.size() * sizeof(int)) == 0);
168-
169168
}
170169
if (test_section("append"))
171170
{

0 commit comments

Comments
 (0)