BVB Source Codes

uWebSockets Show Extensions.cpp Source code

Return Download uWebSockets: download Extensions.cpp Source code - Download uWebSockets Source code - Type:.cpp
  1. #include "Extensions.h"
  2.  
  3. namespace uWS {
  4.  
  5. enum ExtensionTokens {
  6.     TOK_PERMESSAGE_DEFLATE = 1838,
  7.     TOK_SERVER_NO_CONTEXT_TAKEOVER = 2807,
  8.     TOK_CLIENT_NO_CONTEXT_TAKEOVER = 2783,
  9.     TOK_SERVER_MAX_WINDOW_BITS = 2372,
  10.     TOK_CLIENT_MAX_WINDOW_BITS = 2348
  11. };
  12.  
  13. class ExtensionsParser {
  14. private:
  15.     int *lastInteger = nullptr;
  16.  
  17. public:
  18.     bool perMessageDeflate = false;
  19.     bool serverNoContextTakeover = false;
  20.     bool clientNoContextTakeover = false;
  21.     int serverMaxWindowBits = 0;
  22.     int clientMaxWindowBits = 0;
  23.  
  24.     int getToken(const char *&in, const char *stop);
  25.     ExtensionsParser(const char *data, size_t length);
  26. };
  27.  
  28. int ExtensionsParser::getToken(const char *&in, const char *stop) {
  29.     while (!isalnum(*in) && in != stop) {
  30.         in++;
  31.     }
  32.  
  33.     int hashedToken = 0;
  34.     while (isalnum(*in) || *in == '-' || *in == '_') {
  35.         if (isdigit(*in)) {
  36.             hashedToken = hashedToken * 10 - (*in - '0');
  37.         } else {
  38.             hashedToken += *in;
  39.         }
  40.         in++;
  41.     }
  42.     return hashedToken;
  43. }
  44.  
  45. ExtensionsParser::ExtensionsParser(const char *data, size_t length) {
  46.     const char *stop = data + length;
  47.     int token = 1;
  48.     for (; token && token != TOK_PERMESSAGE_DEFLATE; token = getToken(data, stop));
  49.  
  50.     perMessageDeflate = (token == TOK_PERMESSAGE_DEFLATE);
  51.     while ((token = getToken(data, stop))) {
  52.         switch (token) {
  53.         case TOK_PERMESSAGE_DEFLATE:
  54.             return;
  55.         case TOK_SERVER_NO_CONTEXT_TAKEOVER:
  56.             serverNoContextTakeover = true;
  57.             break;
  58.         case TOK_CLIENT_NO_CONTEXT_TAKEOVER:
  59.             clientNoContextTakeover = true;
  60.             break;
  61.         case TOK_SERVER_MAX_WINDOW_BITS:
  62.             serverMaxWindowBits = 1;
  63.             lastInteger = &serverMaxWindowBits;
  64.             break;
  65.         case TOK_CLIENT_MAX_WINDOW_BITS:
  66.             clientMaxWindowBits = 1;
  67.             lastInteger = &clientMaxWindowBits;
  68.             break;
  69.         default:
  70.             if (token < 0 && lastInteger) {
  71.                 *lastInteger = -token;
  72.             }
  73.             break;
  74.         }
  75.     }
  76. }
  77.  
  78. template <bool isServer>
  79. ExtensionsNegotiator<isServer>::ExtensionsNegotiator(int wantedOptions) {
  80.     options = wantedOptions;
  81. }
  82.  
  83. template <bool isServer>
  84. std::string ExtensionsNegotiator<isServer>::generateOffer() {
  85.     std::string extensionsOffer;
  86.     if (options & Options::PERMESSAGE_DEFLATE) {
  87.         extensionsOffer += "permessage-deflate";
  88.  
  89.         if (options & Options::CLIENT_NO_CONTEXT_TAKEOVER) {
  90.             extensionsOffer += "; client_no_context_takeover";
  91.         }
  92.  
  93.         if (options & Options::SERVER_NO_CONTEXT_TAKEOVER) {
  94.             extensionsOffer += "; server_no_context_takeover";
  95.         }
  96.     }
  97.  
  98.     return extensionsOffer;
  99. }
  100.  
  101. template <bool isServer>
  102. void ExtensionsNegotiator<isServer>::readOffer(std::string offer) {
  103.     if (isServer) {
  104.         ExtensionsParser extensionsParser(offer.data(), offer.length());
  105.         if ((options & PERMESSAGE_DEFLATE) && extensionsParser.perMessageDeflate) {
  106.             if (extensionsParser.clientNoContextTakeover || (options & CLIENT_NO_CONTEXT_TAKEOVER)) {
  107.                 options |= CLIENT_NO_CONTEXT_TAKEOVER;
  108.             }
  109.  
  110.             if (extensionsParser.serverNoContextTakeover) {
  111.                 options |= SERVER_NO_CONTEXT_TAKEOVER;
  112.             } else {
  113.                 options &= ~SERVER_NO_CONTEXT_TAKEOVER;
  114.             }
  115.         } else {
  116.             options &= ~PERMESSAGE_DEFLATE;
  117.         }
  118.     } else {
  119.         // todo!
  120.     }
  121. }
  122.  
  123. template <bool isServer>
  124. int ExtensionsNegotiator<isServer>::getNegotiatedOptions() {
  125.     return options;
  126. }
  127.  
  128. template class ExtensionsNegotiator<true>;
  129. template class ExtensionsNegotiator<false>;
  130.  
  131. }
  132.  
downloadExtensions.cpp Source code - Download uWebSockets Source code
Related Source Codes/Software:
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11

 Back to top