File Coverage

src/panda/protocol/websocket/utils.h
Criterion Covered Total %
statement 15 15 100.0
branch 30 30 100.0
condition n/a
subroutine n/a
pod n/a
total 45 45 100.0


line stmt bran cond sub pod time code
1             #pragma once
2             #include
3             #include
4             #include // strncasecmp
5             #include
6             #include
7              
8             namespace panda { namespace protocol { namespace websocket {
9              
10             uint32_t string_hash32_ci (const char *key, size_t len);
11              
12             struct string_hash_ci {
13             size_t operator() (const string& s) const {
14             return string_hash32_ci(s.data(), s.length());
15             }
16             };
17              
18             struct string_equal_ci {
19             bool operator() (const string& lhs, const string& rhs) const {
20             return lhs.length() == rhs.length() && strncasecmp(lhs.data(), rhs.data(), lhs.length()) == 0;
21             }
22             };
23              
24             struct char_equal_ci {
25             bool operator()(char ch1, char ch2) {
26             return std::toupper(ch1) == std::toupper(ch2);
27             }
28             };
29              
30             inline bool string_contains_ci (const string& haystack, const string& needle) {
31             auto iter = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(), char_equal_ci());
32             return (iter != haystack.end());
33             }
34              
35             template
36 869           inline bool parse_binary_number (T& num, const char*& src, size_t len) {
37 869 100         if (!num && len >= sizeof(T)) { // common case - have whole number in src
    100          
    100          
    100          
    100          
    100          
38 415           num = *((T*)src);
39 415           src += sizeof(T);
40 415           return true;
41             }
42              
43 454           uint8_t bcnt = ((char*)&num)[sizeof(T)-1];
44 454           char* dst = (char*)&num + bcnt;
45              
46 454 100         if (bcnt + len >= sizeof(T)) { // have everything left in src
    100          
    100          
47 118           char* end = (char*)&num + sizeof(T);
48 236 100         while (dst != end) *dst++ = *src++;
    100          
    100          
49 118           return true;
50             }
51              
52             // still not enough
53 336           ((char*)&num)[sizeof(T)-1] = bcnt+len;
54 336           char* end = dst + len;
55 672 100         while (dst != end) *dst++ = *src++;
    100          
    100          
56 336           return false;
57             }
58              
59             void crypt_mask (char* str, size_t len, uint32_t mask, uint64_t bytes_received);
60              
61             string close_message(uint16_t code);
62              
63             /// @return true to use in static variable initializators
64             bool register_close_codes(std::initializer_list>);
65              
66             /// close clode formatter
67             struct ccfmt {
68             uint16_t code;
69             const string& msg;
70              
71             ccfmt(uint16_t code, const string& msg) : code(code), msg(msg) {}
72             };
73              
74             std::ostream& operator<< (std::ostream&, const ccfmt&);
75              
76             }}}