ソースを参照

Test mg_close_connection

bel 9 年 前
コミット
9d7e746d03
1 ファイル変更34 行追加0 行削除
  1. 34 0
      examples/embedded_c/embedded_c.c

+ 34 - 0
examples/embedded_c/embedded_c.c

@@ -63,6 +63,9 @@ ExampleHandler(struct mg_connection *conn, void *cbdata)
 	mg_printf(conn,
 	          "<p>To see a page from the *.foo handler <a "
 	          "href=\"xy.foo\">click xy.foo</a></p>");
+	mg_printf(
+	    conn,
+	    "<p>To see a page from the close handler <a href=\"close\">click close</a></p>");
 	mg_printf(conn,
 	          "<p>To see a page from the FileHandler handler <a "
 	          "href=\"form\">click form</a> (this is the form test page)</p>");
@@ -145,6 +148,34 @@ FooHandler(struct mg_connection *conn, void *cbdata)
 
 
 int
+CloseHandler(struct mg_connection *conn, void *cbdata)
+{
+	/* Handler may access the request info using mg_get_request_info */
+	const struct mg_request_info *req_info = mg_get_request_info(conn);
+
+	mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
+	mg_printf(conn, "<html><body>");
+	mg_printf(conn, "<h2>This handler will close the connection in a second</h2>");
+#ifdef _WIN32
+		Sleep(1000);
+#else
+		sleep(1);
+#endif
+	mg_printf(conn, "bye");
+    printf("CloseHandler: close connection\n");
+    mg_close_connection(conn);
+    printf("CloseHandler: wait 10 sec\n");
+#ifdef _WIN32
+		Sleep(10000);
+#else
+		sleep(10);
+#endif
+    printf("CloseHandler: return from function\n");
+	return 1;
+}
+
+
+int
 FileHandler(struct mg_connection *conn, void *cbdata)
 {
 	/* In this handler, we ignore the req_info and send the file "fileName". */
@@ -474,6 +505,9 @@ main(int argc, char *argv[])
 	/* Add handler for all files with .foo extention */
 	mg_set_request_handler(ctx, "**.foo$", FooHandler, 0);
 
+	/* Add handler for /close extention */
+	mg_set_request_handler(ctx, "/close", CloseHandler, 0);
+
 	/* Add handler for /form  (serve a file outside the document root) */
 	mg_set_request_handler(ctx,
 	                       "/form",