Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1 | From 4cab5e5c0c5f19fcee7d37b4a38b156d63a150d4 Mon Sep 17 00:00:00 2001 |
| 2 | From: Peter Thorson <git@zaphoyd.com> |
| 3 | Date: Sun, 11 Jun 2017 16:13:25 -0500 |
| 4 | Subject: [PATCH] minor adjustments to recent extension negotiation related |
| 5 | fixes, refactor a bit more extension negotiation code to be simpler |
| 6 | |
| 7 | --- |
| 8 | websocketpp/impl/connection_impl.hpp | 6 +-- |
| 9 | websocketpp/processors/hybi13.hpp | 92 ++++++++++++++++++------------------ |
| 10 | 2 files changed, 49 insertions(+), 49 deletions(-) |
| 11 | |
| 12 | Index: websocketpp-0.7.0/websocketpp/impl/connection_impl.hpp |
| 13 | =================================================================== |
| 14 | --- websocketpp-0.7.0.orig/websocketpp/impl/connection_impl.hpp |
| 15 | +++ websocketpp-0.7.0/websocketpp/impl/connection_impl.hpp |
| 16 | @@ -1222,17 +1222,17 @@ |
| 17 | std::pair<lib::error_code,std::string> neg_results; |
| 18 | neg_results = m_processor->negotiate_extensions(m_request); |
| 19 | |
| 20 | - if (neg_results.first == error::make_error_code(error::extension_parse_error)) { |
| 21 | + if (neg_results.first == processor::error::make_error_code(processor::error::extension_parse_error)) { |
| 22 | // There was a fatal error in extension parsing that should result in |
| 23 | // a failed connection attempt. |
| 24 | - m_alog.write(log::alevel::info, "Bad request: " + neg_results.first.message()); |
| 25 | + m_elog.write(log::elevel::info, "Bad request: " + neg_results.first.message()); |
| 26 | m_response.set_status(http::status_code::bad_request); |
| 27 | return neg_results.first; |
| 28 | } else if (neg_results.first) { |
| 29 | // There was a fatal error in extension processing that is probably our |
| 30 | // fault. Consider extension negotiation to have failed and continue as |
| 31 | // if extensions were not supported |
| 32 | - m_alog.write(log::alevel::info, |
| 33 | + m_elog.write(log::elevel::info, |
| 34 | "Extension negotiation failed: " + neg_results.first.message()); |
| 35 | } else { |
| 36 | // extension negotiation succeeded, set response header accordingly |
| 37 | Index: websocketpp-0.7.0/websocketpp/processors/hybi13.hpp |
| 38 | =================================================================== |
| 39 | --- websocketpp-0.7.0.orig/websocketpp/processors/hybi13.hpp |
| 40 | +++ websocketpp-0.7.0/websocketpp/processors/hybi13.hpp |
| 41 | @@ -97,11 +97,6 @@ |
| 42 | /** |
| 43 | * This exists mostly because the code for requests and responses is |
| 44 | * identical and I can't have virtual template methods. |
| 45 | - * |
| 46 | - * NOTE: this method makes assumptions that the permessage-deflate |
| 47 | - * extension is the only one supported. If additional extensions are |
| 48 | - * ever supported it should be reviewed carefully. Most cases where |
| 49 | - * that assumption is made are explicitly noted. |
| 50 | */ |
| 51 | template <typename header_type> |
| 52 | err_str_pair negotiate_extensions_helper(header_type const & header) { |
| 53 | @@ -130,55 +125,60 @@ |
| 54 | |
| 55 | http::parameter_list::const_iterator it; |
| 56 | |
| 57 | + // look through the list of extension requests to find the first |
| 58 | + // one that we can accept. |
| 59 | if (m_permessage_deflate.is_implemented()) { |
| 60 | err_str_pair neg_ret; |
| 61 | for (it = p.begin(); it != p.end(); ++it) { |
| 62 | - // look through each extension, if the key is permessage-deflate |
| 63 | - if (it->first == "permessage-deflate") { |
| 64 | - // if we have already successfully negotiated this extension |
| 65 | - // then skip any other requests to negotiate the same one |
| 66 | - // with different parameters |
| 67 | - if (m_permessage_deflate.is_enabled()) { |
| 68 | - continue; |
| 69 | - } |
| 70 | - |
| 71 | - |
| 72 | - neg_ret = m_permessage_deflate.negotiate(it->second); |
| 73 | - |
| 74 | - if (neg_ret.first) { |
| 75 | - // Figure out if this is an error that should halt all |
| 76 | - // extension negotiations or simply cause negotiation of |
| 77 | - // this specific extension to fail. |
| 78 | - //std::cout << "permessage-compress negotiation failed: " |
| 79 | - // << neg_ret.first.message() << std::endl; |
| 80 | - } else { |
| 81 | - // Note: this list will need commas if WebSocket++ ever |
| 82 | - // supports more than one extension |
| 83 | - |
| 84 | - // Actually try to initialize the extension before we |
| 85 | - // deem negotiation complete |
| 86 | - ret.first = m_permessage_deflate.init(base::m_server); |
| 87 | - if (!ret.first) { |
| 88 | - |
| 89 | - // TODO: support multiple extensions. |
| 90 | - // right now, because there is only one extension |
| 91 | - // supported, it failing to negotiate means we are |
| 92 | - // done with all negotiating. In the future if more |
| 93 | - // extensions are supported a better solution will |
| 94 | - // be needed here. |
| 95 | - break; |
| 96 | - } else { |
| 97 | - ret.second += neg_ret.second; |
| 98 | - |
| 99 | - // continue looking for more extensions |
| 100 | - continue; |
| 101 | - } |
| 102 | - |
| 103 | - } |
| 104 | + // not a permessage-deflate extension request, ignore |
| 105 | + if (it->first != "permessage-deflate") { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + // if we have already successfully negotiated this extension |
| 110 | + // then skip any other requests to negotiate the same one |
| 111 | + // with different parameters |
| 112 | + if (m_permessage_deflate.is_enabled()) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + // attempt to negotiate this offer |
| 117 | + neg_ret = m_permessage_deflate.negotiate(it->second); |
| 118 | + |
| 119 | + if (neg_ret.first) { |
| 120 | + // negotiation offer failed. Do nothing. We will continue |
| 121 | + // searching for a permessage-deflate config that succeeds |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + // Negotiation tentatively succeeded |
| 126 | + |
| 127 | + // Actually try to initialize the extension before we |
| 128 | + // deem negotiation complete |
| 129 | + lib::error_code ec = m_permessage_deflate.init(base::m_server); |
| 130 | + |
| 131 | + if (ec) { |
| 132 | + // Negotiation succeeded but initialization failed this is |
| 133 | + // an error that should stop negotiation of permessage |
| 134 | + // deflate. Return the reason for the init failure |
| 135 | + |
| 136 | + ret.first = ec; |
| 137 | + break; |
| 138 | + } else { |
| 139 | + // Successfully initialized, push the negotiated response into |
| 140 | + // the reply and stop looking for additional permessage-deflate |
| 141 | + // extensions |
| 142 | + ret.second += neg_ret.second; |
| 143 | + break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | + // support for future extensions would go here. Should check the value of |
| 149 | + // ret.first before continuing. Might need to consider whether failure of |
| 150 | + // negotiation of an earlier extension should stop negotiation of subsequent |
| 151 | + // ones |
| 152 | + |
| 153 | return ret; |
| 154 | } |
| 155 | |