Skip to content

Commit 7c2e592

Browse files
committed
test http on windows.
1 parent 0faa565 commit 7c2e592

10 files changed

+208
-90
lines changed

CppNet.sln

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EchoClient", "test\echo\Ech
2020
EndProject
2121
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EchoServer", "test\echo\EchoServer.vcxproj", "{5C8EF7D1-E00B-43B7-8A49-38A68157ADC5}"
2222
EndProject
23+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "http", "http", "{9A610A2A-2F46-4390-AB37-42589C3FA503}"
24+
EndProject
25+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "http", "test\http\http.vcxproj", "{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}"
26+
EndProject
2327
Global
2428
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2529
Debug|x64 = Debug|x64
@@ -68,6 +72,14 @@ Global
6872
{5C8EF7D1-E00B-43B7-8A49-38A68157ADC5}.Release|x64.Build.0 = Release|x64
6973
{5C8EF7D1-E00B-43B7-8A49-38A68157ADC5}.Release|x86.ActiveCfg = Release|Win32
7074
{5C8EF7D1-E00B-43B7-8A49-38A68157ADC5}.Release|x86.Build.0 = Release|Win32
75+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Debug|x64.ActiveCfg = Debug|x64
76+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Debug|x64.Build.0 = Debug|x64
77+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Debug|x86.ActiveCfg = Debug|Win32
78+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Debug|x86.Build.0 = Debug|Win32
79+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Release|x64.ActiveCfg = Release|x64
80+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Release|x64.Build.0 = Release|x64
81+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Release|x86.ActiveCfg = Release|Win32
82+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72}.Release|x86.Build.0 = Release|Win32
7183
EndGlobalSection
7284
GlobalSection(SolutionProperties) = preSolution
7385
HideSolutionNode = FALSE
@@ -76,6 +88,8 @@ Global
7688
{2F87BAE1-DE01-410E-9DC2-34B5188D928F} = {76E82C5D-E3D2-4DA1-9F61-FB3D244C4BBC}
7789
{25B16872-AB29-4667-B345-04979B149DD7} = {2F87BAE1-DE01-410E-9DC2-34B5188D928F}
7890
{5C8EF7D1-E00B-43B7-8A49-38A68157ADC5} = {2F87BAE1-DE01-410E-9DC2-34B5188D928F}
91+
{9A610A2A-2F46-4390-AB37-42589C3FA503} = {76E82C5D-E3D2-4DA1-9F61-FB3D244C4BBC}
92+
{1C9F6CAE-56CD-4508-A3C0-0FC1A4A3DF72} = {9A610A2A-2F46-4390-AB37-42589C3FA503}
7993
EndGlobalSection
8094
GlobalSection(ExtensibilityGlobals) = postSolution
8195
SolutionGuid = {8F945FF5-0662-454F-9E54-7CB45BABF6D6}

base/Buffer.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ int CBuffer::ReadUntil(char* res, int len) {
357357

358358
int CBuffer::ReadUntil(char* res, int len, const char* find, int find_len, int& need_len) {
359359
int size = FindStr(find, find_len);
360-
size += find_len;
361360
if (size) {
362361
if (size <= len) {
363362
return Read(res, size);

net/win/SocketImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ void CSocketImpl::_Send(base::CMemSharePtr<CEventHandler>& event) {
197197

198198
} else if (!event->_off_set) {
199199
if (_post_event_num == 0) {
200-
err |= ERR_CONNECT_CLOSE |;
200+
err |= ERR_CONNECT_CLOSE;
201201
}
202202
}
203203

test/echo/EchoServer.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
<EnableCOMDATFolding>true</EnableCOMDATFolding>
113113
<OptimizeReferences>true</OptimizeReferences>
114114
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
115+
<AdditionalDependencies>CppNet.lib;%(AdditionalDependencies)</AdditionalDependencies>
115116
</Link>
116117
</ItemDefinitionGroup>
117118
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -127,6 +128,7 @@
127128
<EnableCOMDATFolding>true</EnableCOMDATFolding>
128129
<OptimizeReferences>true</OptimizeReferences>
129130
<AdditionalLibraryDirectories>$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
131+
<AdditionalDependencies>CppNet.lib;%(AdditionalDependencies)</AdditionalDependencies>
130132
</Link>
131133
</ItemDefinitionGroup>
132134
<ItemGroup>

test/http/HttpContext.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "HttpContext.h"
44

55
const char CRLF[] = "\r\n";
6-
const int CRLF_LEN = sizeof(CRLF);
6+
const int CRLF_LEN = 2;
7+
const int VERSION_LEN = sizeof("HTTP/1.1");
78

89
bool CHttpContext::processRequestLine(const char* begin, const char* end) {
910
bool succeed = false;
@@ -23,19 +24,18 @@ bool CHttpContext::processRequestLine(const char* begin, const char* end) {
2324
}
2425

2526
start = space + 1;
26-
succeed = end - start == 8 && std::equal(start, end - 1, "HTTP/1.");
27-
if (succeed) {
28-
if (*(end-1) == '1') {
29-
_request.SetVersion(Http11);
27+
const char* version_end = start + VERSION_LEN - 1;
28+
succeed = true;
29+
if (std::equal(start, version_end, "HTTP/1.1")) {
30+
_request.SetVersion(Http11);
3031

31-
} else if (*(end-1) == '0') {
32-
_request.SetVersion(Http10);
32+
} else if (std::equal(start, version_end, "HTTP/1.0")) {
33+
_request.SetVersion(Http10);
3334

34-
} else {
35-
succeed = false;
36-
}
37-
}
38-
}
35+
} else {
36+
succeed = false;
37+
}
38+
}
3939
}
4040
return succeed;
4141
}

test/http/HttpRequest.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#ifndef TEST_HTTP_HTTP_REQUEST_HEADER
22
#define TEST_HTTP_HTTP_REQUEST_HEADER
33

4+
#ifndef __linux__
5+
#include <string.h>
6+
#define isspace(c) (c == ' ')
7+
#endif
8+
49
#include <map>
510
#include <stdio.h>
611
#include <assert.h>

test/http/HttpResponse.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdio.h>
2+
#include <string>
23
#include "HttpResponse.h"
34

45
std::string CHttpResponse::GetSendBuffer() const {

test/http/HttpServerTest.cpp

+14-76
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#include <iostream>
22
#include <map>
3-
#include <binders.h>
3+
#include <fstream>
44

55
#include "HttpResponse.h"
66
#include "HttpServer.h"
77
#include "HttpRequest.h"
88

9-
extern char favicon[555];
9+
std::string image;
1010
bool benchmark = false;
1111

12+
std::string GetFile();
1213
void OnRequest(const CHttpRequest& req, CHttpResponse& resp) {
1314
std::cout << "Headers " << req.GetMethodString() << " " << req.GetPath() << std::endl;
1415
if (!benchmark) {
@@ -33,7 +34,10 @@ void OnRequest(const CHttpRequest& req, CHttpResponse& resp) {
3334
resp.SetStatusCode(k200Ok);
3435
resp.SetStatusMessage("OK");
3536
resp.SetContentType("image/png");
36-
resp.SetBody(std::string(favicon, sizeof favicon));
37+
if (image.empty()) {
38+
image = GetFile();
39+
}
40+
resp.SetBody(image);
3741

3842
} else if (req.GetPath() == "/hello") {
3943
resp.SetStatusCode(k200Ok);
@@ -49,6 +53,13 @@ void OnRequest(const CHttpRequest& req, CHttpResponse& resp) {
4953
}
5054
}
5155

56+
std::string GetFile() {
57+
std::ifstream t("logo.png");
58+
std::string str((std::istreambuf_iterator<char>(t)),
59+
std::istreambuf_iterator<char>());
60+
return std::move(str);
61+
}
62+
5263
void DisConnectionFunc(const cppnet::Handle& handle, uint32_t err) {
5364
std::cout << "[DisConnectionFunc]" << std::endl;
5465
}
@@ -69,76 +80,3 @@ int main() {
6980

7081
cppnet::Join();
7182
}
72-
73-
char favicon[555] = {
74-
'\x89', 'P', 'N', 'G', '\xD', '\xA', '\x1A', '\xA',
75-
'\x0', '\x0', '\x0', '\xD', 'I', 'H', 'D', 'R',
76-
'\x0', '\x0', '\x0', '\x10', '\x0', '\x0', '\x0', '\x10',
77-
'\x8', '\x6', '\x0', '\x0', '\x0', '\x1F', '\xF3', '\xFF',
78-
'a', '\x0', '\x0', '\x0', '\x19', 't', 'E', 'X',
79-
't', 'S', 'o', 'f', 't', 'w', 'a', 'r',
80-
'e', '\x0', 'A', 'd', 'o', 'b', 'e', '\x20',
81-
'I', 'm', 'a', 'g', 'e', 'R', 'e', 'a',
82-
'd', 'y', 'q', '\xC9', 'e', '\x3C', '\x0', '\x0',
83-
'\x1', '\xCD', 'I', 'D', 'A', 'T', 'x', '\xDA',
84-
'\x94', '\x93', '9', 'H', '\x3', 'A', '\x14', '\x86',
85-
'\xFF', '\x5D', 'b', '\xA7', '\x4', 'R', '\xC4', 'm',
86-
'\x22', '\x1E', '\xA0', 'F', '\x24', '\x8', '\x16', '\x16',
87-
'v', '\xA', '6', '\xBA', 'J', '\x9A', '\x80', '\x8',
88-
'A', '\xB4', 'q', '\x85', 'X', '\x89', 'G', '\xB0',
89-
'I', '\xA9', 'Q', '\x24', '\xCD', '\xA6', '\x8', '\xA4',
90-
'H', 'c', '\x91', 'B', '\xB', '\xAF', 'V', '\xC1',
91-
'F', '\xB4', '\x15', '\xCF', '\x22', 'X', '\x98', '\xB',
92-
'T', 'H', '\x8A', 'd', '\x93', '\x8D', '\xFB', 'F',
93-
'g', '\xC9', '\x1A', '\x14', '\x7D', '\xF0', 'f', 'v',
94-
'f', '\xDF', '\x7C', '\xEF', '\xE7', 'g', 'F', '\xA8',
95-
'\xD5', 'j', 'H', '\x24', '\x12', '\x2A', '\x0', '\x5',
96-
'\xBF', 'G', '\xD4', '\xEF', '\xF7', '\x2F', '6', '\xEC',
97-
'\x12', '\x20', '\x1E', '\x8F', '\xD7', '\xAA', '\xD5', '\xEA',
98-
'\xAF', 'I', '5', 'F', '\xAA', 'T', '\x5F', '\x9F',
99-
'\x22', 'A', '\x2A', '\x95', '\xA', '\x83', '\xE5', 'r',
100-
'9', 'd', '\xB3', 'Y', '\x96', '\x99', 'L', '\x6',
101-
'\xE9', 't', '\x9A', '\x25', '\x85', '\x2C', '\xCB', 'T',
102-
'\xA7', '\xC4', 'b', '1', '\xB5', '\x5E', '\x0', '\x3',
103-
'h', '\x9A', '\xC6', '\x16', '\x82', '\x20', 'X', 'R',
104-
'\x14', 'E', '6', 'S', '\x94', '\xCB', 'e', 'x',
105-
'\xBD', '\x5E', '\xAA', 'U', 'T', '\x23', 'L', '\xC0',
106-
'\xE0', '\xE2', '\xC1', '\x8F', '\x0', '\x9E', '\xBC', '\x9',
107-
'A', '\x7C', '\x3E', '\x1F', '\x83', 'D', '\x22', '\x11',
108-
'\xD5', 'T', '\x40', '\x3F', '8', '\x80', 'w', '\xE5',
109-
'3', '\x7', '\xB8', '\x5C', '\x2E', 'H', '\x92', '\x4',
110-
'\x87', '\xC3', '\x81', '\x40', '\x20', '\x40', 'g', '\x98',
111-
'\xE9', '6', '\x1A', '\xA6', 'g', '\x15', '\x4', '\xE3',
112-
'\xD7', '\xC8', '\xBD', '\x15', '\xE1', 'i', '\xB7', 'C',
113-
'\xAB', '\xEA', 'x', '\x2F', 'j', 'X', '\x92', '\xBB',
114-
'\x18', '\x20', '\x9F', '\xCF', '3', '\xC3', '\xB8', '\xE9',
115-
'N', '\xA7', '\xD3', 'l', 'J', '\x0', 'i', '6',
116-
'\x7C', '\x8E', '\xE1', '\xFE', 'V', '\x84', '\xE7', '\x3C',
117-
'\x9F', 'r', '\x2B', '\x3A', 'B', '\x7B', '7', 'f',
118-
'w', '\xAE', '\x8E', '\xE', '\xF3', '\xBD', 'R', '\xA9',
119-
'd', '\x2', 'B', '\xAF', '\x85', '2', 'f', 'F',
120-
'\xBA', '\xC', '\xD9', '\x9F', '\x1D', '\x9A', 'l', '\x22',
121-
'\xE6', '\xC7', '\x3A', '\x2C', '\x80', '\xEF', '\xC1', '\x15',
122-
'\x90', '\x7', '\x93', '\xA2', '\x28', '\xA0', 'S', 'j',
123-
'\xB1', '\xB8', '\xDF', '\x29', '5', 'C', '\xE', '\x3F',
124-
'X', '\xFC', '\x98', '\xDA', 'y', 'j', 'P', '\x40',
125-
'\x0', '\x87', '\xAE', '\x1B', '\x17', 'B', '\xB4', '\x3A',
126-
'\x3F', '\xBE', 'y', '\xC7', '\xA', '\x26', '\xB6', '\xEE',
127-
'\xD9', '\x9A', '\x60', '\x14', '\x93', '\xDB', '\x8F', '\xD',
128-
'\xA', '\x2E', '\xE9', '\x23', '\x95', '\x29', 'X', '\x0',
129-
'\x27', '\xEB', 'n', 'V', 'p', '\xBC', '\xD6', '\xCB',
130-
'\xD6', 'G', '\xAB', '\x3D', 'l', '\x7D', '\xB8', '\xD2',
131-
'\xDD', '\xA0', '\x60', '\x83', '\xBA', '\xEF', '\x5F', '\xA4',
132-
'\xEA', '\xCC', '\x2', 'N', '\xAE', '\x5E', 'p', '\x1A',
133-
'\xEC', '\xB3', '\x40', '9', '\xAC', '\xFE', '\xF2', '\x91',
134-
'\x89', 'g', '\x91', '\x85', '\x21', '\xA8', '\x87', '\xB7',
135-
'X', '\x7E', '\x7E', '\x85', '\xBB', '\xCD', 'N', 'N',
136-
'b', 't', '\x40', '\xFA', '\x93', '\x89', '\xEC', '\x1E',
137-
'\xEC', '\x86', '\x2', 'H', '\x26', '\x93', '\xD0', 'u',
138-
'\x1D', '\x7F', '\x9', '2', '\x95', '\xBF', '\x1F', '\xDB',
139-
'\xD7', 'c', '\x8A', '\x1A', '\xF7', '\x5C', '\xC1', '\xFF',
140-
'\x22', 'J', '\xC3', '\x87', '\x0', '\x3', '\x0', 'K',
141-
'\xBB', '\xF8', '\xD6', '\x2A', 'v', '\x98', 'I', '\x0',
142-
'\x0', '\x0', '\x0', 'I', 'E', 'N', 'D', '\xAE',
143-
'B', '\x60', '\x82',
144-
};

0 commit comments

Comments
 (0)