Skip to content

Commit de856c8

Browse files
author
ithewei
committed
SSE: Content-Type: text/event-stream
1 parent 314e6ca commit de856c8

File tree

9 files changed

+29
-9
lines changed

9 files changed

+29
-9
lines changed

examples/httpd/handler.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,16 @@ int Handler::upload(const HttpContextPtr& ctx) {
284284
}
285285
return response_status(ctx, status_code);
286286
}
287+
288+
int Handler::sse(const HttpContextPtr& ctx) {
289+
ctx->writer->EndHeaders("Content-Type", "text/event-stream");
290+
// send(time) every 1s
291+
hv::setInterval(1000, [ctx](hv::TimerID timerID) {
292+
char szTime[DATETIME_FMT_BUFLEN] = {0};
293+
datetime_t now = datetime_now();
294+
datetime_fmt(&now, szTime);
295+
ctx->writer->write("event: timeout\n");
296+
ctx->writer->write(hv::asprintf("data: %s\n\n", szTime));
297+
});
298+
return HTTP_STATUS_UNFINISHED;
299+
}

examples/httpd/handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class Handler {
2626

2727
static int login(const HttpContextPtr& ctx);
2828
static int upload(const HttpContextPtr& ctx);
29+
// SSE: Server Send Events
30+
static int sse(const HttpContextPtr& ctx);
2931

3032
private:
3133
static int response_status(HttpResponse* resp, int code = 200, const char* message = NULL) {

examples/httpd/router.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ void Router::Register(hv::HttpService& router) {
110110
router.POST("/json", Handler::json);
111111

112112
// Content-Type: multipart/form-data
113-
// bin/curl -v http://ip:port/form -F "user=admin pswd=123456"
113+
// bin/curl -v http://ip:port/form -F 'user=admin' -F 'pswd=123456'
114114
router.POST("/form", Handler::form);
115115

116116
// curl -v http://ip:port/test -H "Content-Type:application/x-www-form-urlencoded" -d 'bool=1&int=123&float=3.14&string=hello'
117117
// curl -v http://ip:port/test -H "Content-Type:application/json" -d '{"bool":true,"int":123,"float":3.14,"string":"hello"}'
118-
// bin/curl -v http://ip:port/test -F 'bool=1 int=123 float=3.14 string=hello'
118+
// bin/curl -v http://ip:port/test -F 'bool=1' -F 'int=123' -F 'float=3.14' -F 'string=hello'
119119
router.POST("/test", Handler::test);
120120

121121
// Content-Type: application/grpc
@@ -130,7 +130,10 @@ void Router::Register(hv::HttpService& router) {
130130
// curl -v http://ip:port/login -H "Content-Type:application/json" -d '{"username":"admin","password":"123456"}'
131131
router.POST("/login", Handler::login);
132132

133-
// curl -v http://ip:port/upload -d "hello,world!"
134-
// curl -v http://ip:port/upload -F "file=@LICENSE"
133+
// curl -v http://ip:port/upload -d '@LICENSE'
134+
// curl -v http://ip:port/upload -F 'file=@LICENSE'
135135
router.POST("/upload", Handler::upload);
136+
137+
// SSE: Server Send Events
138+
router.GET("/SSE", Handler::sse);
136139
}

http/HttpMessage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ void HttpMessage::FillContentLength() {
322322
DumpBody();
323323
content_length = body.size();
324324
}
325-
if (iter == headers.end() && !IsChunked()) {
325+
if (iter == headers.end() && !IsChunked() && content_type != TEXT_EVENT_STREAM) {
326326
if (content_length != 0 || type == HTTP_RESPONSE) {
327327
headers["Content-Length"] = hv::to_string(content_length);
328328
}

http/HttpMessage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class HV_EXPORT HttpRequest : public HttpMessage {
432432
}
433433

434434
// scheme
435-
bool isHttps() {
435+
bool IsHttps() {
436436
return strncmp(scheme.c_str(), "https", 5) == 0 ||
437437
strncmp(url.c_str(), "https://", 8) == 0;
438438
}

http/client/AsyncHttpClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int AsyncHttpClient::doTask(const HttpClientTaskPtr& task) {
4747
hio_set_peeraddr(connio, &peeraddr.sa, sockaddr_len(&peeraddr));
4848
addChannel(connio);
4949
// https
50-
if (req->isHttps()) {
50+
if (req->IsHttps()) {
5151
hio_enable_ssl(connio);
5252
}
5353
}

http/client/http_client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static int http_client_make_request(http_client_t* cli, HttpRequest* req) {
166166
req->port = cli->port;
167167
}
168168

169-
bool https = req->isHttps();
169+
bool https = req->IsHttps();
170170
bool use_proxy = https ? (!cli->https_proxy_host.empty()) : (!cli->http_proxy_host.empty());
171171
if (use_proxy) {
172172
if (req->host == "127.0.0.1" || req->host == "localhost") {
@@ -452,7 +452,7 @@ int __http_client_send(http_client_t* cli, HttpRequest* req, HttpResponse* resp)
452452
int err = 0;
453453
int timeout = req->timeout;
454454
int connfd = cli->fd;
455-
bool https = req->isHttps();
455+
bool https = req->IsHttps();
456456
bool keepalive = true;
457457

458458
time_t start_time = time(NULL);

http/httpdef.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ enum http_method {
149149
XX(TEXT_PLAIN, text/plain, txt) \
150150
XX(TEXT_HTML, text/html, html) \
151151
XX(TEXT_CSS, text/css, css) \
152+
XX(TEXT_EVENT_STREAM, text/event-stream, sse) \
152153
XX(IMAGE_JPEG, image/jpeg, jpg) \
153154
XX(IMAGE_PNG, image/png, png) \
154155
XX(IMAGE_GIF, image/gif, gif) \

http/server/HttpResponseWriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class HttpResponseWriter : public SocketChannel {
2727
// Begin -> End
2828
// Begin -> WriteResponse -> End
2929
// Begin -> WriteStatus -> WriteHeader -> WriteBody -> End
30+
// Begin -> EndHeaders("Content-Type", "text/event-stream") -> write -> write -> ... -> close
3031
// Begin -> EndHeaders("Content-Length", content_length) -> WriteBody -> WriteBody -> ... -> End
3132
// Begin -> EndHeaders("Transfer-Encoding", "chunked") -> WriteChunked -> WriteChunked -> ... -> End
3233

0 commit comments

Comments
 (0)