浏览代码

Add response timeout function (issue #48)

Add the code by rscreene contributed by rscreene to civetweb.
This cannot be done with automatic merge of the pull request, since the repositorie seem to be too different. So it is reimplemented at the current main line, with some adjustments.
bel 10 年之前
父节点
当前提交
423c3233c0
共有 2 个文件被更改,包括 37 次插入7 次删除
  1. 17 2
      include/civetweb.h
  2. 20 5
      src/civetweb.c

+ 17 - 2
include/civetweb.h

@@ -616,8 +616,23 @@ CIVETWEB_API struct mg_connection *mg_connect_websocket_client(const char *host,
 CIVETWEB_API struct mg_connection *mg_connect_client(const char *host, int port, int use_ssl,
                                                char *error_buffer, size_t error_buffer_size);
 
-/* TODO: */
-/* CIVETWEB_API int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len); */
+
+enum {
+    TIMEOUT_INFINITE = -1
+};
+
+/* Wait for a response from the server
+   Parameters:
+     conn: connection
+     ebuf, ebuf_len: error message placeholder.
+     timeout: time to wait for a response in milliseconds (if < 0 then wait forever)
+
+   Return:
+     On success, >= 0
+     On error/timeout, < 0
+*/
+CIVETWEB_API int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len, int timeout);
+
 
 #ifdef __cplusplus
 }

+ 20 - 5
src/civetweb.c

@@ -6479,9 +6479,22 @@ static int is_valid_uri(const char *uri)
     return uri[0] == '/' || (uri[0] == '*' && uri[1] == '\0');
 }
 
-static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len)
+static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len, int timeout)
 {
     const char *cl;
+    struct pollfd pfd;
+
+    if (timeout >= 0) {
+        pfd.fd = conn->client.sock;
+        switch (poll(&pfd, 1, timeout)) {
+        case 0:
+            snprintf(ebuf, ebuf_len, "%s", "Timed out");
+            return 0;
+        case -1:
+            snprintf(ebuf, ebuf_len, "%s", "Interrupted");
+            return 0;
+        }
+    }
 
     ebuf[0] = '\0';
     reset_per_request_attributes(conn);
@@ -6519,10 +6532,12 @@ static int getreq(struct mg_connection *conn, char *ebuf, size_t ebuf_len)
     return ebuf[0] == '\0';
 }
 
-int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len)
+int mg_get_response(struct mg_connection *conn, char *ebuf, size_t ebuf_len, int timeout)
 {
     /* Implementation of API function for HTTP clients */
-    return getreq(conn, ebuf, ebuf_len);
+    /* TODO: Define proper return values - maybe return length?
+             For the first test use <0 for error and >0 for OK */
+    return (getreq(conn, ebuf, ebuf_len, timeout) == 0) ? -1 : +1;
 }
 
 struct mg_connection *mg_download(const char *host, int port, int use_ssl,
@@ -6538,7 +6553,7 @@ struct mg_connection *mg_download(const char *host, int port, int use_ssl,
     } else if (mg_vprintf(conn, fmt, ap) <= 0) {
         snprintf(ebuf, ebuf_len, "%s", "Error sending request");
     } else {
-        getreq(conn, ebuf, ebuf_len);
+        getreq(conn, ebuf, ebuf_len, TIMEOUT_INFINITE);
     }
     if (ebuf[0] != '\0' && conn != NULL) {
         mg_close_connection(conn);
@@ -6661,7 +6676,7 @@ static void process_new_connection(struct mg_connection *conn)
        to crule42. */
     conn->data_len = 0;
     do {
-        if (!getreq(conn, ebuf, sizeof(ebuf))) {
+        if (!getreq(conn, ebuf, sizeof(ebuf), TIMEOUT_INFINITE)) {
             send_http_error(conn, 500, "Server Error", "%s", ebuf);
             conn->must_close = 1;
         } else if (!is_valid_uri(conn->request_info.uri)) {