Skip to content

Commit 7c03672

Browse files
authored
fixed binary encoding issue (#17)
1 parent fc53190 commit 7c03672

File tree

6 files changed

+101
-33
lines changed

6 files changed

+101
-33
lines changed

example/frontend/cat.jpg

150 KB
Loading

example/frontend/index.html

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
<!DOCTYPE html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8">
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>WebFrame</title>
7-
<script src="form.js"></script>
8-
</head>
9-
<body>
10-
<header>
11-
<h1>WebFrame</h1>
12-
</header>
13-
14-
<form id="name-form" onsubmit="handleNameSubmit(event)">
15-
<label for="name">Name</label>
16-
<input id="name" name="name" type="text" required>
17-
<button type="submit">Submit</button>
18-
</form>
19-
20-
<footer>
21-
<p>&copy; 2026 Maxtek Consulting LLC. All rights reserved.</p>
22-
</footer>
23-
</body>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>WebFrame Example</title>
8+
<link rel="stylesheet" href="style.css" />
9+
<script src="form.js"></script>
10+
</head>
11+
<body>
12+
<img src="cat.jpg" alt="WebFrame Logo" class="logo" width="20%"/>
13+
<div class="divClass">
14+
<form id="name-form" onsubmit="handleNameSubmit(event)">
15+
<label for="name">Name</label>
16+
<input id="name" name="name" type="text" required>
17+
<button type="submit">Submit</button>
18+
</form>
19+
</div>
20+
</body>
2421
</html>

example/frontend/style.css

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
html,
2+
body {
3+
height: 100%;
4+
margin: 0;
5+
font-family: Arial, sans-serif;
6+
}
7+
8+
body {
9+
display: flex;
10+
flex-direction: column;
11+
align-items: center;
12+
justify-content: center;
13+
background: linear-gradient(to bottom, darkcyan, darkgray);
14+
/* Gradient background */
15+
}
16+
17+
.logo {
18+
margin-bottom: 24px;
19+
}
20+
21+
.divClass {
22+
text-align: center;
23+
color: #ffffff;
24+
/* White text color */
25+
font-size: 2.5rem;
26+
/* Adjusted font size */
27+
font-weight: 700;
28+
/* Bold text */
29+
background: rgba(0, 0, 0, 0.6);
30+
/* Semi-transparent background */
31+
padding: 20px;
32+
border-radius: 10px;
33+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
34+
}
35+
36+
.divClass div {
37+
margin-bottom: 10px;
38+
}
39+
40+
.divClass div:last-child {
41+
margin-bottom: 0;
42+
font-size: 1.5rem;
43+
/* Smaller font size for the second line */
44+
}

example/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class archive_handler : public webframe::handler
4040
else {
4141
res->set_status(404);
4242
res->set_header("Content-Type", "text/plain");
43-
const std::string not_found_msg = "404 Not Found";
43+
const std::string not_found_msg = "Not Found";
4444
res->set_body(reinterpret_cast<const uint8_t*>(not_found_msg.data()), not_found_msg.size());
4545
}
4646
}

src/runtimes/desktop/desktop.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <webframe.hpp>
55

66
#include <wx/wx.h>
7+
#include <wx/mstream.h>
78
#include <wx/webview.h>
89
#include <wx/uri.h>
910

src/runtimes/desktop/response.cpp

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
#include <desktop.hpp>
22

3+
class response_body_writer : public wxWebViewHandlerResponseData
4+
{
5+
public:
6+
response_body_writer() = default;
7+
~response_body_writer() = default;
8+
9+
wxInputStream *GetStream() override
10+
{
11+
if (!_stream)
12+
{
13+
_stream = std::make_unique<wxMemoryInputStream>(_buffer.data(), _buffer.size());
14+
}
15+
return _stream.get();
16+
}
17+
18+
void write(const uint8_t *data, size_t size)
19+
{
20+
_buffer.insert(_buffer.end(), data, data + size);
21+
}
22+
23+
private:
24+
std::vector<uint8_t> _buffer;
25+
std::unique_ptr<wxMemoryInputStream> _stream;
26+
};
27+
28+
329
namespace webframe
430
{
531
namespace desktop
@@ -20,27 +46,27 @@ namespace webframe
2046

2147
void response::set_body(const uint8_t *data, size_t size)
2248
{
23-
_response->Finish(std::string(reinterpret_cast<const char *>(data), size));
49+
std::unique_ptr<response_body_writer> body_writer = std::make_unique<response_body_writer>();
50+
body_writer->write(data, size);
51+
_response->Finish(wxSharedPtr<wxWebViewHandlerResponseData>(body_writer.release()));
2452
_sent = true;
2553
}
2654

2755
void response::write_body(const std::function<bool(std::pair<const uint8_t *, size_t> &)> &callback)
2856
{
29-
std::vector<uint8_t> buffer;
30-
bool has_more_data = true;
31-
while (has_more_data)
57+
std::unique_ptr<response_body_writer> body_writer = std::make_unique<response_body_writer>();
58+
bool has_more(true);
59+
while(has_more)
3260
{
3361
std::pair<const uint8_t *, size_t> data;
34-
if (callback(data))
35-
{
36-
buffer.insert(buffer.end(), data.first, data.first + data.second);
37-
}
38-
else
62+
has_more = callback(data);
63+
if (data.first && data.second)
3964
{
40-
has_more_data = false;
65+
body_writer->write(data.first, data.second);
4166
}
4267
}
43-
set_body(buffer.data(), buffer.size());
68+
_response->Finish(wxSharedPtr<wxWebViewHandlerResponseData>(body_writer.release()));
69+
_sent = true;
4470
}
4571
}
4672
}

0 commit comments

Comments
 (0)