Skip to content

Commit 0423358

Browse files
committed
changed simplesvr to compute md5 of attached file. Fixed decompressor to work with multiple payloads. Tested with huge gzipped files uploaded with curl
1 parent 9b4c2d3 commit 0423358

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

example/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
CXXFLAGS = -std=c++14 -I.. -Wall -Wextra -pthread
44
#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib -lssl -lcrypto
55
#OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -lssl -lcrypto
6-
OPENSSL_SUPPORT =
6+
OPENSSL_SUPPORT =
77
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
88

99
all: server client hello simplesvr redirect benchmark
@@ -18,7 +18,7 @@ hello : hello.cc ../httplib.h Makefile
1818
$(CXX) -o hello $(CXXFLAGS) hello.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
1919

2020
simplesvr : simplesvr.cc ../httplib.h Makefile
21-
$(CXX) -o simplesvr $(CXXFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)
21+
$(CXX) -o simplesvr $(CXXFLAGS) simplesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) -lcrypto
2222

2323
redirect : redirect.cc ../httplib.h Makefile
2424
$(CXX) -o redirect $(CXXFLAGS) redirect.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT)

example/simplesvr.cc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <cstdio>
99
#include <httplib.h>
1010
#include <iostream>
11+
#include <sstream>
12+
#include <iomanip>
13+
#include <openssl/md5.h>
1114

1215
#define SERVER_CERT_FILE "./cert.pem"
1316
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
@@ -110,19 +113,32 @@ int main(int argc, const char **argv) {
110113
});
111114

112115
svr.Post("/multi2", [](Stream& strm, const Request &req, Response &res) {
113-
std::string rsp="req body stream is :\n";
116+
std::ostringstream rsp;
117+
rsp << "req body stream:\n";
118+
MD5_CTX ctx;
119+
MD5_Init(&ctx);
120+
int nCall=0;
114121

115122
detail::read_content(
116123
strm, req, std::numeric_limits<size_t>::max(), res.status
117-
, req.progress, [&rsp] (const char *buf, size_t n){
118-
rsp+="cut(";
119-
rsp.append(buf,n);
120-
rsp+=")\n";
124+
, req.progress, [&] (const char *buf, size_t n){
125+
MD5_Update(&ctx, buf, n);
126+
nCall++;
121127
return true;
122128
}
123129
);
130+
unsigned char result[MD5_DIGEST_LENGTH+1];
124131

125-
res.set_content(rsp, "text/plain");
132+
rsp << "nCall:" << nCall <<std::endl;
133+
134+
MD5_Final(result, &ctx);
135+
rsp << "md5:" ;
136+
for(int i =0; i<MD5_DIGEST_LENGTH; i++) {
137+
rsp << std::hex << std::setw(2) << std::setfill('0') << (int) result[i];
138+
}
139+
rsp << std::endl;
140+
141+
res.set_content(rsp.str(), "text/plain");
126142
});
127143

128144
svr.set_error_handler([](const Request & /*req*/, Response &res) {

httplib.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,11 +1367,10 @@ class decompressor {
13671367
case Z_DATA_ERROR:
13681368
case Z_MEM_ERROR: inflateEnd(&strm); return false;
13691369
}
1370-
13711370
if (!callback(buff, bufsiz - strm.avail_out)) { return false; }
13721371
} while (strm.avail_out == 0);
13731372

1374-
return ret == Z_STREAM_END;
1373+
return true;
13751374
}
13761375

13771376
private:

0 commit comments

Comments
 (0)