aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2021-05-12 20:11:32 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2021-05-12 20:11:32 +0300
commit9ac7492bb57e5545b7ca3d6f840b47b2b90c33a0 (patch)
tree11e8bd3dbef11f17151ef90621297be57da64cca
parentfc0385be98417b72d2aedb29b28a1da0e700ae09 (diff)
downloadlibmicrohttpd-9ac7492bb57e5545b7ca3d6f840b47b2b90c33a0.tar.gz
libmicrohttpd-9ac7492bb57e5545b7ca3d6f840b47b2b90c33a0.zip
Further clarified requirement to use MHD_get_timeout()
Emphasise that it is necessary to use MHD_get_timeout() with any external sockets polling function.
-rw-r--r--src/include/microhttpd.h102
-rw-r--r--src/microhttpd/daemon.c81
2 files changed, 130 insertions, 53 deletions
diff --git a/src/include/microhttpd.h b/src/include/microhttpd.h
index 54a3afa7..44156aab 100644
--- a/src/include/microhttpd.h
+++ b/src/include/microhttpd.h
@@ -2121,8 +2121,13 @@ enum MHD_DaemonInfoType
2121 /** 2121 /**
2122 * Request the file descriptor for the external epoll. 2122 * Request the file descriptor for the external epoll.
2123 * No extra arguments should be passed. 2123 * No extra arguments should be passed.
2124 *
2124 * Waiting on epoll FD must not block longer than value 2125 * Waiting on epoll FD must not block longer than value
2125 * returned by #MHD_get_timeout(). 2126 * returned by #MHD_get_timeout() otherwise connections
2127 * will "hung" with unprocessed data in network buffers
2128 * and timed-out connections will not be closed.
2129 *
2130 * @sa #MHD_get_timeout(), #MHD_run()
2126 */ 2131 */
2127 MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY, 2132 MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY,
2128 MHD_DAEMON_INFO_EPOLL_FD = MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY, 2133 MHD_DAEMON_INFO_EPOLL_FD = MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY,
@@ -2557,10 +2562,12 @@ MHD_add_connection (struct MHD_Daemon *daemon,
2557 * 2562 *
2558 * This function should only be called in when MHD is configured to 2563 * This function should only be called in when MHD is configured to
2559 * use external select with 'select()' or with 'epoll'. 2564 * use external select with 'select()' or with 'epoll'.
2560 * In the latter case, it will only add the single 'epoll()' file 2565 * In the latter case, it will only add the single 'epoll' file
2561 * descriptor used by MHD to the sets. 2566 * descriptor used by MHD to the sets.
2562 * It's necessary to use #MHD_get_timeout() in combination with 2567 * It's necessary to use #MHD_get_timeout() to get maximum timeout
2563 * this function. 2568 * value for `select()`. Usage of `select()` with indefinite timeout
2569 * (or timeout larger than returned by #MHD_get_timeout()) will
2570 * violate MHD API and may results in pending unprocessed data.
2564 * 2571 *
2565 * This function must be called only for daemon started 2572 * This function must be called only for daemon started
2566 * without #MHD_USE_INTERNAL_POLLING_THREAD flag. 2573 * without #MHD_USE_INTERNAL_POLLING_THREAD flag.
@@ -2598,8 +2605,10 @@ MHD_get_fdset (struct MHD_Daemon *daemon,
2598 * use external select with 'select()' or with 'epoll'. 2605 * use external select with 'select()' or with 'epoll'.
2599 * In the latter case, it will only add the single 'epoll' file 2606 * In the latter case, it will only add the single 'epoll' file
2600 * descriptor used by MHD to the sets. 2607 * descriptor used by MHD to the sets.
2601 * It's necessary to use #MHD_get_timeout() in combination with 2608 * It's necessary to use #MHD_get_timeout() to get maximum timeout
2602 * this function. 2609 * value for `select()`. Usage of `select()` with indefinite timeout
2610 * (or timeout larger than returned by #MHD_get_timeout()) will
2611 * violate MHD API and may results in pending unprocessed data.
2603 * 2612 *
2604 * This function must be called only for daemon started 2613 * This function must be called only for daemon started
2605 * without #MHD_USE_INTERNAL_POLLING_THREAD flag. 2614 * without #MHD_USE_INTERNAL_POLLING_THREAD flag.
@@ -2632,10 +2641,17 @@ MHD_get_fdset2 (struct MHD_Daemon *daemon,
2632 * daemon FDs in fd_sets, call FD_ZERO for each fd_set 2641 * daemon FDs in fd_sets, call FD_ZERO for each fd_set
2633 * before calling this function. Size of fd_set is 2642 * before calling this function. Size of fd_set is
2634 * determined by current value of FD_SETSIZE. 2643 * determined by current value of FD_SETSIZE.
2635 * It's necessary to use #MHD_get_timeout() in combination with
2636 * this function.
2637 * 2644 *
2638 * This function could be called only for daemon started 2645 * This function should only be called in when MHD is configured to
2646 * use external select with 'select()' or with 'epoll'.
2647 * In the latter case, it will only add the single 'epoll' file
2648 * descriptor used by MHD to the sets.
2649 * It's necessary to use #MHD_get_timeout() to get maximum timeout
2650 * value for `select()`. Usage of `select()` with indefinite timeout
2651 * (or timeout larger than returned by #MHD_get_timeout()) will
2652 * violate MHD API and may results in pending unprocessed data.
2653 *
2654 * This function must be called only for daemon started
2639 * without #MHD_USE_INTERNAL_POLLING_THREAD flag. 2655 * without #MHD_USE_INTERNAL_POLLING_THREAD flag.
2640 * 2656 *
2641 * @param daemon daemon to get sets from 2657 * @param daemon daemon to get sets from
@@ -2657,20 +2673,31 @@ MHD_get_fdset2 (struct MHD_Daemon *daemon,
2657 2673
2658/** 2674/**
2659 * Obtain timeout value for polling function for this daemon. 2675 * Obtain timeout value for polling function for this daemon.
2660 * This function set value to amount of milliseconds for which polling 2676 *
2661 * function (`select()` or `poll()`) should at most block, not the 2677 * This function set value to the amount of milliseconds for which polling
2678 * function (`select()`, `poll()` or epoll) should at most block, not the
2662 * timeout value set for connections. 2679 * timeout value set for connections.
2663 * It is important to always use this function, even if connection 2680 *
2664 * timeout is not set, as in some cases MHD may already have more 2681 * Any external polling function must be called with the timeout value
2665 * data to process on next turn (data pending in TLS buffers, 2682 * provided by this function. Smaller timeout values can be used for polling
2666 * connections are already ready with epoll etc.) and returned timeout 2683 * function if it is required for any reason, but using larger timeout value
2667 * will be zero. 2684 * or no timeout (indefinite timeout) when this function return #MHD_YES
2685 * will break MHD processing logic and result in "hung" connections with
2686 * data pending in network buffers and other problems.
2687 *
2688 * It is important to always use this function when external polling is
2689 * used. If this function returns #MHD_YES then #MHD_run() (or
2690 * #MHD_run_from_select()) must be called right after return from polling
2691 * function, regardless of the states of MHD fds.
2692 *
2693 * In practice, if #MHD_YES is returned then #MHD_run() (or
2694 * #MHD_run_from_select()) must be called not later than @a timeout
2695 * millisecond.
2668 * 2696 *
2669 * @param daemon daemon to query for timeout 2697 * @param daemon daemon to query for timeout
2670 * @param timeout set to the timeout (in milliseconds) 2698 * @param timeout set to the timeout (in milliseconds)
2671 * @return #MHD_YES on success, #MHD_NO if timeouts are 2699 * @return #MHD_YES on success, #MHD_NO if timeouts are
2672 * not used (or no connections exist that would 2700 * not used and no data is pending.
2673 * necessitate the use of a timeout right now).
2674 * @ingroup event 2701 * @ingroup event
2675 */ 2702 */
2676_MHD_EXTERN enum MHD_Result 2703_MHD_EXTERN enum MHD_Result
@@ -2679,19 +2706,26 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
2679 2706
2680 2707
2681/** 2708/**
2682 * Run webserver operations (without blocking unless in client 2709 * Run webserver operations (without blocking unless in client callbacks).
2683 * callbacks). This method should be called by clients in combination 2710 *
2684 * with #MHD_get_fdset if the client-controlled select method is used and 2711 * This method should be called by clients in combination with
2685 * #MHD_get_timeout(). 2712 * #MHD_get_fdset() (or #MHD_get_daemon_info() with MHD_DAEMON_INFO_EPOLL_FD
2713 * if epoll is used) and #MHD_get_timeout() if the client-controlled
2714 * connection polling method is used (i.e. daemon was started without
2715 * #MHD_USE_INTERNAL_POLLING_THREAD flag).
2686 * 2716 *
2687 * This function is a convenience method, which is useful if the 2717 * This function is a convenience method, which is useful if the
2688 * fd_sets from #MHD_get_fdset were not directly passed to `select()`; 2718 * fd_sets from #MHD_get_fdset were not directly passed to `select()`;
2689 * with this function, MHD will internally do the appropriate `select()` 2719 * with this function, MHD will internally do the appropriate `select()`
2690 * call itself again. While it is always safe to call #MHD_run (if 2720 * call itself again. While it is acceptable to call #MHD_run (if
2691 * #MHD_USE_INTERNAL_POLLING_THREAD is not set), you should call 2721 * #MHD_USE_INTERNAL_POLLING_THREAD is not set) at any moment, you should
2692 * #MHD_run_from_select if performance is important (as it saves an 2722 * call #MHD_run_from_select() if performance is important (as it saves an
2693 * expensive call to `select()`). 2723 * expensive call to `select()`).
2694 * 2724 *
2725 * If #MHD_get_timeout() returned #MHD_YES, than this function must be called
2726 * right after polling function returns regardless of detected activity on
2727 * the daemon's FDs.
2728 *
2695 * @param daemon daemon to run 2729 * @param daemon daemon to run
2696 * @return #MHD_YES on success, #MHD_NO if this 2730 * @return #MHD_YES on success, #MHD_NO if this
2697 * daemon was not started with the right 2731 * daemon was not started with the right
@@ -2704,14 +2738,20 @@ MHD_run (struct MHD_Daemon *daemon);
2704 2738
2705/** 2739/**
2706 * Run websever operation with possible blocking. 2740 * Run websever operation with possible blocking.
2707 * This function do the following: waits for any network event not more than 2741 *
2708 * specified number of milliseconds, processes all incoming and outgoing 2742 * This function does the following: waits for any network event not more
2709 * data, processes new connections, processes any timed-out connection, and 2743 * than specified number of milliseconds, processes all incoming and
2710 * do other things required to run webserver. 2744 * outgoing data, processes new connections, processes any timed-out
2745 * connection, and does other things required to run webserver.
2711 * Once all connections are processed, function returns. 2746 * Once all connections are processed, function returns.
2747 *
2712 * This function is useful for quick and simple webserver implementation if 2748 * This function is useful for quick and simple webserver implementation if
2713 * application needs to run a single thread only and does not have any other 2749 * application needs to run a single thread only and does not have any other
2714 * network activity. 2750 * network activity.
2751 *
2752 * It is expected that the external socket polling function is not used in
2753 * conjunction with this function unless the @a millisec is set to zero.
2754 *
2715 * @param daemon the daemon to run 2755 * @param daemon the daemon to run
2716 * @param millisec the maximum time in milliseconds to wait for network and 2756 * @param millisec the maximum time in milliseconds to wait for network and
2717 * other events. Note: there is no guarantee that function 2757 * other events. Note: there is no guarantee that function
@@ -2747,6 +2787,10 @@ MHD_run_wait (struct MHD_Daemon *daemon,
2747 * not have to call `select()` again to determine which operations are 2787 * not have to call `select()` again to determine which operations are
2748 * ready. 2788 * ready.
2749 * 2789 *
2790 * If #MHD_get_timeout() returned #MHD_YES, than this function must be
2791 * called right after `select()` returns regardless of detected activity
2792 * on the daemon's FDs.
2793 *
2750 * This function cannot be used with daemon started with 2794 * This function cannot be used with daemon started with
2751 * #MHD_USE_INTERNAL_POLLING_THREAD flag. 2795 * #MHD_USE_INTERNAL_POLLING_THREAD flag.
2752 * 2796 *
diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c
index 5e7bd384..d6042356 100644
--- a/src/microhttpd/daemon.c
+++ b/src/microhttpd/daemon.c
@@ -703,8 +703,10 @@ MHD_TLS_init (struct MHD_Daemon *daemon)
703 * use external select with 'select()' or with 'epoll'. 703 * use external select with 'select()' or with 'epoll'.
704 * In the latter case, it will only add the single 'epoll' file 704 * In the latter case, it will only add the single 'epoll' file
705 * descriptor used by MHD to the sets. 705 * descriptor used by MHD to the sets.
706 * It's necessary to use #MHD_get_timeout() in combination with 706 * It's necessary to use #MHD_get_timeout() to get maximum timeout
707 * this function. 707 * value for `select()`. Usage of `select()` with indefinite timeout
708 * (or timeout larger than returned by #MHD_get_timeout()) will
709 * violate MHD API and may results in pending unprocessed data.
708 * 710 *
709 * This function must be called only for daemon started 711 * This function must be called only for daemon started
710 * without #MHD_USE_INTERNAL_POLLING_THREAD flag. 712 * without #MHD_USE_INTERNAL_POLLING_THREAD flag.
@@ -1104,8 +1106,10 @@ internal_get_fdset2 (struct MHD_Daemon *daemon,
1104 * use external select with 'select()' or with 'epoll'. 1106 * use external select with 'select()' or with 'epoll'.
1105 * In the latter case, it will only add the single 'epoll' file 1107 * In the latter case, it will only add the single 'epoll' file
1106 * descriptor used by MHD to the sets. 1108 * descriptor used by MHD to the sets.
1107 * It's necessary to use #MHD_get_timeout() in combination with 1109 * It's necessary to use #MHD_get_timeout() to get maximum timeout
1108 * this function. 1110 * value for `select()`. Usage of `select()` with indefinite timeout
1111 * (or timeout larger than returned by #MHD_get_timeout()) will
1112 * violate MHD API and may results in pending unprocessed data.
1109 * 1113 *
1110 * This function must be called only for daemon started 1114 * This function must be called only for daemon started
1111 * without #MHD_USE_INTERNAL_POLLING_THREAD flag. 1115 * without #MHD_USE_INTERNAL_POLLING_THREAD flag.
@@ -3772,14 +3776,26 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
3772 3776
3773/** 3777/**
3774 * Obtain timeout value for polling function for this daemon. 3778 * Obtain timeout value for polling function for this daemon.
3775 * This function set value to amount of milliseconds for which polling 3779 *
3776 * function (`select()` or `poll()`) should at most block, not the 3780 * This function set value to the amount of milliseconds for which polling
3781 * function (`select()`, `poll()` or epoll) should at most block, not the
3777 * timeout value set for connections. 3782 * timeout value set for connections.
3778 * It is important to always use this function, even if connection 3783 *
3779 * timeout is not set as in some cases MHD may already have more 3784 * Any external polling function must be called with the timeout value
3780 * data to process on next turn (data pending in TLS buffers, 3785 * provided by this function. Smaller timeout values can be used for polling
3781 * connections are already ready with epoll etc.) and returned timeout 3786 * function if it is required for any reason, but using larger timeout value
3782 * will be zero. 3787 * or no timeout (indefinite timeout) when this function return #MHD_YES
3788 * will break MHD processing logic and result in "hung" connections with
3789 * data pending in network buffers and other problems.
3790 *
3791 * It is important to always use this function when external polling is
3792 * used. If this function returns #MHD_YES then #MHD_run() (or
3793 * #MHD_run_from_select()) must be called right after return from polling
3794 * function, regardless of the states of MHD fds.
3795 *
3796 * In practice, if #MHD_YES is returned then #MHD_run() (or
3797 * #MHD_run_from_select()) must be called not later than @a timeout
3798 * millisecond.
3783 * @remark To be called only from thread that process 3799 * @remark To be called only from thread that process
3784 * daemon's select()/poll()/etc. 3800 * daemon's select()/poll()/etc.
3785 * 3801 *
@@ -4010,8 +4026,12 @@ internal_run_from_select (struct MHD_Daemon *daemon,
4010 * not have to call `select()` again to determine which operations are 4026 * not have to call `select()` again to determine which operations are
4011 * ready. 4027 * ready.
4012 * 4028 *
4029 * If #MHD_get_timeout() returned #MHD_YES, than this function must be
4030 * called right after `select()` returns regardless of detected activity
4031 * on the daemon's FDs.
4032 *
4013 * This function cannot be used with daemon started with 4033 * This function cannot be used with daemon started with
4014 * MHD_USE_INTERNAL_POLLING_THREAD flag. 4034 * #MHD_USE_INTERNAL_POLLING_THREAD flag.
4015 * 4035 *
4016 * @param daemon daemon to run select loop for 4036 * @param daemon daemon to run select loop for
4017 * @param read_fd_set read set 4037 * @param read_fd_set read set
@@ -5073,18 +5093,25 @@ MHD_epoll (struct MHD_Daemon *daemon,
5073 5093
5074 5094
5075/** 5095/**
5076 * Run webserver operations (without blocking unless in client 5096 * Run webserver operations (without blocking unless in client callbacks).
5077 * callbacks). This method should be called by clients in combination 5097 *
5078 * with #MHD_get_fdset if the client-controlled select method is used and 5098 * This method should be called by clients in combination with
5079 * #MHD_get_timeout(). 5099 * #MHD_get_fdset() (or #MHD_get_daemon_info() with MHD_DAEMON_INFO_EPOLL_FD
5100 * if epoll is used) and #MHD_get_timeout() if the client-controlled
5101 * connection polling method is used (i.e. daemon was started without
5102 * #MHD_USE_INTERNAL_POLLING_THREAD flag).
5080 * 5103 *
5081 * This function is a convenience method, which is useful if the 5104 * This function is a convenience method, which is useful if the
5082 * fd_sets from #MHD_get_fdset were not directly passed to `select()`; 5105 * fd_sets from #MHD_get_fdset were not directly passed to `select()`;
5083 * with this function, MHD will internally do the appropriate `select()` 5106 * with this function, MHD will internally do the appropriate `select()`
5084 * call itself again. While it is always safe to call #MHD_run (in 5107 * call itself again. While it is acceptable to call #MHD_run (if
5085 * external select mode), you should call #MHD_run_from_select if 5108 * #MHD_USE_INTERNAL_POLLING_THREAD is not set) at any moment, you should
5086 * performance is important (as it saves an expensive call to 5109 * call #MHD_run_from_select() if performance is important (as it saves an
5087 * `select()`). 5110 * expensive call to `select()`).
5111 *
5112 * If #MHD_get_timeout() returned #MHD_YES, than this function must be called
5113 * right after polling function returns regardless of detected activity on
5114 * the daemon's FDs.
5088 * 5115 *
5089 * @param daemon daemon to run 5116 * @param daemon daemon to run
5090 * @return #MHD_YES on success, #MHD_NO if this 5117 * @return #MHD_YES on success, #MHD_NO if this
@@ -5106,14 +5133,20 @@ MHD_run (struct MHD_Daemon *daemon)
5106 5133
5107/** 5134/**
5108 * Run websever operation with possible blocking. 5135 * Run websever operation with possible blocking.
5109 * This function do the following: waits for any network event not more than 5136 *
5110 * specified number of milliseconds, processes all incoming and outgoing 5137 * This function does the following: waits for any network event not more
5111 * data, processes new connections, processes any timed-out connection, and 5138 * than specified number of milliseconds, processes all incoming and
5112 * do other things required to run webserver. 5139 * outgoing data, processes new connections, processes any timed-out
5140 * connection, and does other things required to run webserver.
5113 * Once all connections are processed, function returns. 5141 * Once all connections are processed, function returns.
5142 *
5114 * This function is useful for quick and simple webserver implementation if 5143 * This function is useful for quick and simple webserver implementation if
5115 * application needs to run a single thread only and does not have any other 5144 * application needs to run a single thread only and does not have any other
5116 * network activity. 5145 * network activity.
5146 *
5147 * It is expected that the external socket polling function is not used in
5148 * conjunction with this function unless the @a millisec is set to zero.
5149 *
5117 * @param daemon the daemon to run 5150 * @param daemon the daemon to run
5118 * @param millisec the maximum time in milliseconds to wait for network and 5151 * @param millisec the maximum time in milliseconds to wait for network and
5119 * other events. Note: there is no guarantee that function 5152 * other events. Note: there is no guarantee that function