ithewei 6 年之前
父節點
當前提交
c460e45491
共有 4 個文件被更改,包括 109 次插入80 次删除
  1. 41 25
      base/hdef.h
  2. 6 6
      base/hmath.h
  3. 55 49
      http/http_content.cpp
  4. 7 0
      misc/grpc_server.h

+ 41 - 25
base/hdef.h

@@ -63,18 +63,6 @@ typedef void (*procedure_t)(void* userdata);
 #define INFINITE    (uint32_t)-1
 #endif
 
-#ifndef CR
-#define CR      '\r'
-#endif
-
-#ifndef LF
-#define LF      '\n'
-#endif
-
-#ifndef CRLF
-#define CRLF    "\r\n"
-#endif
-
 #ifndef IN
 #define IN
 #endif
@@ -111,27 +99,43 @@ typedef void (*procedure_t)(void* userdata);
 #define LIMIT(lower, v, upper) ((v) < (lower) ? (lower) : (v) > (upper) ? (upper) : (v))
 #endif
 
-#ifndef SWAP
-#define SWAP(type, a, b) \
-    do {\
-        type x = a;\
-        a = b;\
-        b = x;\
-    } while(0)
-#endif
-
 #ifndef BITSET
 #define BITSET(p, n) (*(p) |= (1u << (n)))
 #endif
 
-#ifndef BITCLEAR
-#define BITCLEAR(p, n) (*(p) &= ~(1u << (n)))
+#ifndef BITCLR
+#define BITCLR(p, n) (*(p) &= ~(1u << (n)))
 #endif
 
 #ifndef BITGET
 #define BITGET(i, n) ((i) & (1u << (n)))
 #endif
 
+// ASCII:
+// [0, 0x20)    control-charaters
+// [0x20, 0x7F) printable-charaters
+//
+// 0x0A => LF
+// 0x0D => CR
+// 0x20 => SPACE
+// 0x7F => DEL
+//
+// [0x09, 0x0D] => \t\n\v\f\r
+// [0x30, 0x39] => 0~9
+// [0x41, 0x5A] => A~Z
+// [0x61, 0x7A] => a~z
+#ifndef CR
+#define CR      '\r'
+#endif
+
+#ifndef LF
+#define LF      '\n'
+#endif
+
+#ifndef CRLF
+#define CRLF    "\r\n"
+#endif
+
 #ifndef LOWER
 #define LOWER(c)    ((c) | 0x20)
 #endif
@@ -144,8 +148,16 @@ typedef void (*procedure_t)(void* userdata);
 #define IS_NUM(c)   ((c) >= '0' && (c) <= '9')
 #endif
 
+#ifndef IS_UPPER
+#define IS_UPPER(c) (((c) >= 'A' && (c) <= 'Z'))
+#endif
+
+#ifndef IS_LOWER
+#define IS_LOWER(c) (((c) >= 'a' && (c) <= 'z'))
+#endif
+
 #ifndef IS_ALPHA
-#define IS_ALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'F'))
+#define IS_ALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
 #endif
 
 #ifndef IS_ALPHANUM
@@ -156,8 +168,12 @@ typedef void (*procedure_t)(void* userdata);
 #define IS_HEX(c) (IS_NUM(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
 #endif
 
+#ifndef IS_CNTRL
+#define IS_CNTRL(c) ((c) >= 0 && (c) < 0x20)
+#endif
+
 #ifndef IS_GRAPH
-#define IS_GRAPH(c) ((c) >= 0x20 && (c) <= 0x7E)
+#define IS_GRAPH(c) ((c) >= 0x20 && (c) < 0x7F)
 #endif
 
 #ifndef ARRAY_SIZE

+ 6 - 6
base/hmath.h

@@ -4,22 +4,22 @@
 
 static inline unsigned long floor2e(unsigned long num) {
     unsigned long n = num;
-    int e = 1;
+    int e = 0;
     while (n>>=1) ++e;
     unsigned long ret = 1;
-    while (--e) ret<<=1;
+    while (e--) ret<<=1;
     return ret;
 }
 
 static inline unsigned long ceil2e(unsigned long num) {
     // 2**0 = 1
-    if (num == 0)   return 1;
-    unsigned long n = num;
+    if (num == 0 || num == 1)   return 1;
+    unsigned long n = num - 1;
     int e = 1;
     while (n>>=1) ++e;
     unsigned long ret = 1;
-    while (--e) ret<<=1;
-    return ret == num ? ret : ret<<1;
+    while (e--) ret<<=1;
+    return ret;
 }
 
 #endif // HW_MATH_H_

+ 55 - 49
http/http_content.cpp

@@ -3,71 +3,72 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include "hdef.h"
 #include "hstring.h"
 
 #include "httpdef.h" // for http_content_type_str_by_suffix
 
-#ifndef LOWER
-#define LOWER(c)    ((c) | 0x20)
-#endif
-
-#ifndef UPPER
-#define UPPER(c)    ((c) & ~0x20)
-#endif
-
-#ifndef IS_NUM
-#define IS_NUM(c)   ((c) >= '0' && (c) <= '9')
-#endif
-
-#ifndef IS_ALPHA
-#define IS_ALPHA(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'F'))
-#endif
-
-#ifndef IS_ALPHANUM
-#define IS_ALPHANUM(c) (IS_NUM(c) || IS_ALPHA(c))
-#endif
-
-#ifndef IS_HEX
-#define IS_HEX(c) (IS_NUM(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
-#endif
-
-#ifndef C2I
-#define C2I(c)  ((c)-'0')
-#endif
-
 static char hex2i(char hex) {
     if (hex >= '0' && hex <= '9') {
         return hex - '0';
     }
-    if (hex >= 'A' && hex <= 'F') {
-        return hex - 'A';
-    }
-    if (hex >= 'a' && hex <= 'f') {
-        return hex - 'a';
+    switch (hex) {
+        case 'A': case 'a': return 10;
+        case 'B': case 'b': return 11;
+        case 'C': case 'c': return 12;
+        case 'D': case 'd': return 13;
+        case 'E': case 'e': return 14;
+        case 'F': case 'f': return 15;
+        default: break;
     }
     return 0;
 }
 
+/*
+bool Curl_isunreserved(unsigned char in)
+{
+    switch(in) {
+    case '0': case '1': case '2': case '3': case '4':
+    case '5': case '6': case '7': case '8': case '9':
+    case 'a': case 'b': case 'c': case 'd': case 'e':
+    case 'f': case 'g': case 'h': case 'i': case 'j':
+    case 'k': case 'l': case 'm': case 'n': case 'o':
+    case 'p': case 'q': case 'r': case 's': case 't':
+    case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
+    case 'A': case 'B': case 'C': case 'D': case 'E':
+    case 'F': case 'G': case 'H': case 'I': case 'J':
+    case 'K': case 'L': case 'M': case 'N': case 'O':
+    case 'P': case 'Q': case 'R': case 'S': case 'T':
+    case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
+    case '-': case '.': case '_': case '~':
+      return TRUE;
+    default:
+      break;
+    }
+    return FLASE;
+}
+*/
+
+static inline bool is_unambiguous(char c) {
+    return IS_ALPHANUM(c) ||
+           c == '-' ||
+           c == '_' ||
+           c == '.' ||
+           c == '~';
+}
+
 // scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
 static std::string escape(const std::string& param) {
     std::string str;
     const char* p = param.c_str();
     char escape[4] = {0};
     while (*p != '\0') {
-        if (*p == ' ' ||
-            *p == ':' ||
-            *p == '/' ||
-            *p == '@' ||
-            *p == '?' ||
-            *p == '=' ||
-            *p == '&' ||
-            *p == '#' ||
-            *p == '%') {
-            sprintf(escape, "%%%02X", *p);
-            str += escape;
+        if (is_unambiguous(*p)) {
+            str += *p;
         }
         else {
-            str += *p;
+            sprintf(escape, "%%%02X", *p);
+            str += escape;
         }
         ++p;
     }
@@ -83,10 +84,11 @@ static std::string unescape(const char* escape_param) {
             IS_HEX(p[2])) {
             str += (hex2i(p[1]) << 4 | hex2i(p[2]));
             p += 3;
-            continue;
         }
-        str += *p;
-        ++p;
+        else {
+            str += *p;
+            ++p;
+        }
     }
     return str;
 }
@@ -105,9 +107,11 @@ std::string dump_query_params(QueryParams& query_params) {
 }
 
 int parse_query_params(const char* query_string, QueryParams& query_params) {
+    printf("%s\n", query_string);
     const char* p = strchr(query_string, '?');
     p = p ? p+1 : query_string;
-    p = unescape(p).c_str();
+    std::string unescape_string = unescape(p);
+    p = unescape_string.c_str();
 
     enum {
         s_key,
@@ -126,10 +130,12 @@ int parse_query_params(const char* query_string, QueryParams& query_params) {
             }
             state = s_key;
             key = p+1;
+            printf("key=%s %p\n", key, key);
         }
         else if (*p == '=') {
             state = s_value;
             value = p+1;
+            printf("value=%s %p\n", value, value);
         }
         else {
             state == s_key ? ++key_len : ++value_len;

+ 7 - 0
misc/grpc_server.h

@@ -7,6 +7,11 @@ using grpc::Server;
 using grpc::Service;
 using grpc::ServerCompletionQueue;
 
+// GRPC_DEFAULT_MAX_RECV_MESSAGE_LENGTH = 4M
+// GRPC_DEFAULT_MAX_SEND_MESSAGE_LENGTH = 4M
+#define GRPC_MAX_RECV_MESSAGE_LENGTH    1<<24 // 16M
+#define GRPC_MAX_SEND_MESSAGE_LENGTH    1<<24 // 16M
+
 class GrpcServer {
 public:
     GrpcServer(int port) : _port(port) {
@@ -16,6 +21,8 @@ public:
         build_.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIME_MS,    30000);
         build_.AddChannelArgument(GRPC_ARG_KEEPALIVE_TIMEOUT_MS, 5000);
         build_.AddChannelArgument(GRPC_ARG_KEEPALIVE_PERMIT_WITHOUT_CALLS, 1);
+        build_.SetMaxReceiveMessageSize(GRPC_MAX_RECV_MESSAGE_LENGTH);
+        build_.SetMaxSendMessageSize(GRPC_MAX_SEND_MESSAGE_LENGTH);
     }
 
     void RegisterService(Service* service) {