diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-03-13 16:43:32 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-03-13 16:49:11 +0300 |
commit | 8105f71cc5239b41e5a17e0b7d41bb92a234ee00 (patch) | |
tree | 42787d67232b0d72e45248baa311b1fd989d0ae2 | |
parent | 3a6b50c8215c1c21caaee89d814af9bde321a2ba (diff) | |
download | libmicrohttpd-8105f71cc5239b41e5a17e0b7d41bb92a234ee00.tar.gz libmicrohttpd-8105f71cc5239b41e5a17e0b7d41bb92a234ee00.zip |
Simplified function for determining needs for reply body and headers
-rw-r--r-- | src/microhttpd/connection.c | 122 |
1 files changed, 68 insertions, 54 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index 0280bdab..af2036a0 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c | |||
@@ -1674,79 +1674,90 @@ connection_switch_from_recv_to_send (struct MHD_Connection *connection) | |||
1674 | 1674 | ||
1675 | 1675 | ||
1676 | /** | 1676 | /** |
1677 | * Check whether reply body-specific headers (namely Content-Length, | 1677 | * This enum type describes requirements for reply body and reply bode-specific |
1678 | * Transfer-Encoding) are needed. | 1678 | * headers (namely Content-Length, Transfer-Encoding). |
1679 | */ | ||
1680 | enum replyBodyUse | ||
1681 | { | ||
1682 | /** | ||
1683 | * No reply body allowed. | ||
1684 | * Reply body headers 'Content-Length:' or 'Transfer-Encoding: chunked' are | ||
1685 | * not allowed as well. | ||
1686 | */ | ||
1687 | RP_BODY_NONE = 0, | ||
1688 | |||
1689 | /** | ||
1690 | * Do not send reply body. | ||
1691 | * Reply body headers 'Content-Length:' or 'Transfer-Encoding: chunked' are | ||
1692 | * allowed, but optional. | ||
1693 | */ | ||
1694 | RP_BODY_HEADERS_ONLY = 1, | ||
1695 | |||
1696 | /** | ||
1697 | * Send reply body and | ||
1698 | * reply body headers 'Content-Length:' or 'Transfer-Encoding: chunked'. | ||
1699 | * Reply body headers are required. | ||
1700 | */ | ||
1701 | RP_BODY_SEND = 2 | ||
1702 | }; | ||
1703 | |||
1704 | |||
1705 | /** | ||
1706 | * Check whether reply body must be used. | ||
1679 | * | 1707 | * |
1680 | * If reply body-specific headers are not needed then body itself | 1708 | * If reply body is needed, it could be zero-sized. |
1681 | * is not allowed as well. | ||
1682 | * When reply body-specific headers are needed, the body itself | ||
1683 | * can be present or not, depending on other conditions. | ||
1684 | * | 1709 | * |
1685 | * @param connection the connection to check | 1710 | * @param connection the connection to check |
1686 | * @return true if reply body-specific headers are needed, | 1711 | * @param rcode the response code |
1687 | * false otherwise. | 1712 | * @return enum value indicating whether response body can be used and |
1688 | * @sa is_reply_body_needed() | 1713 | * whether response body length headers are allowed or required. |
1714 | * @sa is_reply_body_header_needed() | ||
1689 | */ | 1715 | */ |
1690 | static bool | 1716 | static enum replyBodyUse |
1691 | is_reply_body_headers_needed (struct MHD_Connection *connection) | 1717 | is_reply_body_needed (struct MHD_Connection *connection, |
1718 | unsigned int rcode) | ||
1692 | { | 1719 | { |
1693 | struct MHD_Connection *const c = connection; /**< a short alias */ | 1720 | struct MHD_Connection *const c = connection; /**< a short alias */ |
1694 | unsigned rcode; /**< the response code */ | ||
1695 | 1721 | ||
1696 | mhd_assert (100 <= c->responseCode); | 1722 | mhd_assert (100 <= rcode); |
1697 | mhd_assert (999 >= c->responseCode); | 1723 | mhd_assert (999 >= rcode); |
1698 | |||
1699 | rcode = (unsigned) c->responseCode; | ||
1700 | 1724 | ||
1701 | if (199 >= rcode) | 1725 | if (199 >= rcode) |
1702 | return false; | 1726 | return RP_BODY_NONE; |
1703 | 1727 | ||
1704 | if (MHD_HTTP_NO_CONTENT == rcode) | 1728 | if (MHD_HTTP_NO_CONTENT == rcode) |
1705 | return false; | 1729 | return RP_BODY_NONE; |
1706 | 1730 | ||
1731 | #if 0 | ||
1732 | /* This check is not needed as upgrade handler is used only with code 101 */ | ||
1707 | #ifdef UPGRADE_SUPPORT | 1733 | #ifdef UPGRADE_SUPPORT |
1708 | if (NULL != c->response->upgrade_handler) | 1734 | if (NULL != response->upgrade_handler) |
1709 | return false; | 1735 | return RP_BODY_NONE; |
1710 | #endif /* UPGRADE_SUPPORT */ | 1736 | #endif /* UPGRADE_SUPPORT */ |
1737 | #endif | ||
1711 | 1738 | ||
1739 | #if 0 | ||
1740 | /* CONNECT is not supported by MHD */ | ||
1741 | /* Successful responses for connect requests are filtered by | ||
1742 | * MHD_queue_response() */ | ||
1712 | if ( (MHD_HTTP_MTHD_CONNECT == c->http_mthd) && | 1743 | if ( (MHD_HTTP_MTHD_CONNECT == c->http_mthd) && |
1713 | (2 == rcode / 100) ) | 1744 | (2 == rcode / 100) ) |
1714 | return false; /* Actually pass-through CONNECT is not supported by MHD */ | 1745 | return false; /* Actually pass-through CONNECT is not supported by MHD */ |
1746 | #endif | ||
1715 | 1747 | ||
1716 | return true; | 1748 | /* Reply body headers could be used. |
1717 | } | 1749 | * Check whether reply body itself must be used. */ |
1718 | |||
1719 | |||
1720 | /** | ||
1721 | * Check whether reply body must be used. | ||
1722 | * | ||
1723 | * If reply body is needed, it could be zero-sized. | ||
1724 | * | ||
1725 | * @param connection the connection to check | ||
1726 | * @return true if reply body must be used, | ||
1727 | * false otherwise | ||
1728 | * @sa is_reply_body_header_needed() | ||
1729 | */ | ||
1730 | static bool | ||
1731 | is_reply_body_needed (struct MHD_Connection *connection) | ||
1732 | { | ||
1733 | struct MHD_Connection *const c = connection; /**< a short alias */ | ||
1734 | unsigned rcode; /**< the response code */ | ||
1735 | |||
1736 | mhd_assert (100 <= c->responseCode); | ||
1737 | mhd_assert (999 >= c->responseCode); | ||
1738 | |||
1739 | if (! is_reply_body_headers_needed (c)) | ||
1740 | return false; | ||
1741 | 1750 | ||
1742 | if (MHD_HTTP_MTHD_HEAD == c->http_mthd) | 1751 | if (MHD_HTTP_MTHD_HEAD == c->http_mthd) |
1743 | return false; | 1752 | return RP_BODY_HEADERS_ONLY; |
1744 | 1753 | ||
1745 | rcode = (unsigned) c->responseCode; | ||
1746 | if (MHD_HTTP_NOT_MODIFIED == rcode) | 1754 | if (MHD_HTTP_NOT_MODIFIED == rcode) |
1747 | return false; | 1755 | return RP_BODY_HEADERS_ONLY; |
1748 | 1756 | ||
1749 | return true; | 1757 | /* Reply body must be sent. The body may have zero length, but body size |
1758 | * must be indicated by headers ('Content-Length:' or | ||
1759 | * 'Transfer-Encoding: chunked'). */ | ||
1760 | return RP_BODY_SEND; | ||
1750 | } | 1761 | } |
1751 | 1762 | ||
1752 | 1763 | ||
@@ -1763,6 +1774,7 @@ setup_reply_properties (struct MHD_Connection *connection) | |||
1763 | { | 1774 | { |
1764 | struct MHD_Connection *const c = connection; /**< a short alias */ | 1775 | struct MHD_Connection *const c = connection; /**< a short alias */ |
1765 | struct MHD_Response *const r = c->response; /**< a short alias */ | 1776 | struct MHD_Response *const r = c->response; /**< a short alias */ |
1777 | enum replyBodyUse use_rp_body; | ||
1766 | bool use_chunked; | 1778 | bool use_chunked; |
1767 | 1779 | ||
1768 | mhd_assert (NULL != r); | 1780 | mhd_assert (NULL != r); |
@@ -1770,11 +1782,13 @@ setup_reply_properties (struct MHD_Connection *connection) | |||
1770 | /* ** Adjust reply properties ** */ | 1782 | /* ** Adjust reply properties ** */ |
1771 | 1783 | ||
1772 | c->keepalive = keepalive_possible (c); | 1784 | c->keepalive = keepalive_possible (c); |
1773 | c->rp_props.use_reply_body_headers = is_reply_body_headers_needed (c); | 1785 | use_rp_body = is_reply_body_needed (c, c->responseCode); |
1774 | if (c->rp_props.use_reply_body_headers) | 1786 | c->rp_props.send_reply_body = (use_rp_body > RP_BODY_HEADERS_ONLY); |
1775 | c->rp_props.send_reply_body = is_reply_body_needed (c); | 1787 | c->rp_props.use_reply_body_headers = (use_rp_body >= RP_BODY_HEADERS_ONLY); |
1776 | else | 1788 | |
1777 | c->rp_props.send_reply_body = false; | 1789 | #ifdef UPGRADE_SUPPORT |
1790 | mhd_assert ((NULL == r->upgrade_handler) || (RP_BODY_NONE == use_rp_body)); | ||
1791 | #endif /* UPGRADE_SUPPORT */ | ||
1778 | 1792 | ||
1779 | if (c->rp_props.use_reply_body_headers) | 1793 | if (c->rp_props.use_reply_body_headers) |
1780 | { | 1794 | { |