PNG  IHDR  8] PLTE S =tRNS   PNG  IHDR  8] PLTE S =tRNS   REDROOM
PHP 7.4.33
Preview: swoole_http2.h Size: 8.38 KB
/opt/cloudlinux/alt-php82/root/usr/include/php/ext/swoole/include/swoole_http2.h

/*
  +----------------------------------------------------------------------+
  | Swoole                                                               |
  +----------------------------------------------------------------------+
  | This source file is subject to version 2.0 of the Apache license,    |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.apache.org/licenses/LICENSE-2.0.html                      |
  | If you did not receive a copy of the Apache2.0 license and are unable|
  | to obtain it through the world-wide-web, please send a note to       |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author: Tianfeng Han  <rango@swoole.com>                             |
  |         Twosee  <twose@qq.com>                                       |
  +----------------------------------------------------------------------+
*/

#pragma once

#include "swoole_protocol.h"

#define SW_HTTP2_PRI_STRING "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"

enum swHttp2ErrorCode {
    SW_HTTP2_ERROR_NO_ERROR = 0x0,
    SW_HTTP2_ERROR_PROTOCOL_ERROR = 0x1,
    SW_HTTP2_ERROR_INTERNAL_ERROR = 0x2,
    SW_HTTP2_ERROR_FLOW_CONTROL_ERROR = 0x3,
    SW_HTTP2_ERROR_SETTINGS_TIMEOUT = 0x4,
    SW_HTTP2_ERROR_STREAM_CLOSED = 0x5,
    SW_HTTP2_ERROR_FRAME_SIZE_ERROR = 0x6,
    SW_HTTP2_ERROR_REFUSED_STREAM = 0x7,
    SW_HTTP2_ERROR_CANCEL = 0x8,
    SW_HTTP2_ERROR_COMPRESSION_ERROR = 0x9,
    SW_HTTP2_ERROR_CONNECT_ERROR = 0xa,
    SW_HTTP2_ERROR_ENHANCE_YOUR_CALM = 0xb,
    SW_HTTP2_ERROR_INADEQUATE_SECURITY = 0xc,
    SW_HTTP2_ERROR_HTTP_1_1_REQUIRED = 0xd,
};

enum swHttp2FrameType {
    SW_HTTP2_TYPE_DATA = 0,
    SW_HTTP2_TYPE_HEADERS = 1,
    SW_HTTP2_TYPE_PRIORITY = 2,
    SW_HTTP2_TYPE_RST_STREAM = 3,
    SW_HTTP2_TYPE_SETTINGS = 4,
    SW_HTTP2_TYPE_PUSH_PROMISE = 5,
    SW_HTTP2_TYPE_PING = 6,
    SW_HTTP2_TYPE_GOAWAY = 7,
    SW_HTTP2_TYPE_WINDOW_UPDATE = 8,
    SW_HTTP2_TYPE_CONTINUATION = 9,
};

enum swHttp2FrameFlag {
    SW_HTTP2_FLAG_NONE = 0x00,
    SW_HTTP2_FLAG_ACK = 0x01,
    SW_HTTP2_FLAG_END_STREAM = 0x01,
    SW_HTTP2_FLAG_END_HEADERS = 0x04,
    SW_HTTP2_FLAG_PADDED = 0x08,
    SW_HTTP2_FLAG_PRIORITY = 0x20,
};

enum swHttp2SettingId {
    SW_HTTP2_SETTING_HEADER_TABLE_SIZE = 0x1,
    SW_HTTP2_SETTINGS_ENABLE_PUSH = 0x2,
    SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS = 0x3,
    SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE = 0x4,
    SW_HTTP2_SETTINGS_MAX_FRAME_SIZE = 0x5,
    SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE = 0x6,
};

enum swHttp2StreamFlag {
    SW_HTTP2_STREAM_NORMAL = 0,
    SW_HTTP2_STREAM_REQUEST_END = 1 << 0,
    SW_HTTP2_STREAM_PIPELINE_REQUEST = 1 << 1,
    SW_HTTP2_STREAM_PIPELINE_RESPONSE = 1 << 2,
    SW_HTTP2_STREAM_USE_PIPELINE_READ = 1 << 3,
};

#define SW_HTTP2_FRAME_HEADER_SIZE 9
#define SW_HTTP2_SETTING_OPTION_SIZE 6
#define SW_HTTP2_SETTING_FRAME_SIZE (SW_HTTP2_FRAME_HEADER_SIZE + SW_HTTP2_SETTING_OPTION_SIZE * 6)
#define SW_HTTP2_FRAME_PING_PAYLOAD_SIZE 8

#define SW_HTTP2_RST_STREAM_SIZE 4
#define SW_HTTP2_PRIORITY_SIZE 5
#define SW_HTTP2_PING_SIZE 8
#define SW_HTTP2_RST_STREAM_SIZE 4
#define SW_HTTP2_GOAWAY_SIZE 8
#define SW_HTTP2_WINDOW_UPDATE_SIZE 4
#define SW_HTTP2_STREAM_ID_SIZE 4
#define SW_HTTP2_SETTINGS_PARAM_SIZE 6

#define swoole_http2_frame_trace_log(_trace_str, ...)                                                                  \
    swoole_trace_log(SW_TRACE_HTTP2,                                                                                   \
                     SW_ECHO_RED_BG " [" SW_ECHO_GREEN "] "                                                            \
                                    "<length=%jd, flags=(%s), stream_id=%d> " _trace_str,                              \
                     " RECV ",                                                                                         \
                     swoole::http2::get_type(type),                                                                    \
                     length,                                                                                           \
                     swoole::http2::get_flag_string(flags).c_str(),                                                    \
                     stream_id,                                                                                        \
                     ##__VA_ARGS__)

#define swoole_http2_send_trace_log(_trace_str, ...)                                                                   \
    swoole_trace_log(SW_TRACE_HTTP2, SW_ECHO_GREEN_BG " " _trace_str, " SEND ", ##__VA_ARGS__)

#define swoole_http2_recv_trace_log(_trace_str, ...)                                                                   \
    swoole_trace_log(SW_TRACE_HTTP2, SW_ECHO_RED_BG " " _trace_str, " RECV ", ##__VA_ARGS__)

namespace swoole {
namespace http2 {

struct Settings {
    uint32_t header_table_size;
    uint32_t enable_push;
    uint32_t max_concurrent_streams;
    uint32_t init_window_size;
    uint32_t max_frame_size;
    uint32_t max_header_list_size;
};

/**
 +-----------------------------------------------+
 |                 Length (24)                   |
 +---------------+---------------+---------------+
 |   Type (8)    |   Flags (8)   |
 +-+-------------+---------------+-------------------------------+
 |R|                 Stream Identifier (31)                      |
 +=+=============================================================+
 |                   Frame Payload (0...)                      ...
 +---------------------------------------------------------------+
 */
struct Frame {
    uint32_t length : 24;
    uint32_t type : 8;
    uint32_t flags : 8;
    uint32_t rsv1 : 1;
    uint32_t identifier : 31;
    char data[0];
};

static sw_inline ssize_t get_length(const char *buf) {
    return (((uint8_t) buf[0]) << 16) + (((uint8_t) buf[1]) << 8) + (uint8_t) buf[2];
}

void put_default_setting(enum swHttp2SettingId id, uint32_t value);
uint32_t get_default_setting(enum swHttp2SettingId id);
size_t pack_setting_frame(char *buf, const Settings &settings, bool server_side);
ssize_t get_frame_length(const Protocol *protocol, network::Socket *conn, PacketLength *pl);
int send_setting_frame(Protocol *protocol, network::Socket *conn);
const char *get_type(int type);
int get_type_color(int type);

static sw_inline void init_settings(Settings *settings) {
    settings->header_table_size = get_default_setting(SW_HTTP2_SETTING_HEADER_TABLE_SIZE);
    settings->enable_push = get_default_setting(SW_HTTP2_SETTINGS_ENABLE_PUSH);
    settings->max_concurrent_streams = get_default_setting(SW_HTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
    settings->init_window_size = get_default_setting(SW_HTTP2_SETTINGS_INIT_WINDOW_SIZE);
    settings->max_frame_size = get_default_setting(SW_HTTP2_SETTINGS_MAX_FRAME_SIZE);
    settings->max_header_list_size = get_default_setting(SW_HTTP2_SETTINGS_MAX_HEADER_LIST_SIZE);
}

static inline const std::string get_flag_string(int __flags) {
    std::string str;
    if (__flags & SW_HTTP2_FLAG_ACK) {
        str.append("ACK|");
    }
    if (__flags & SW_HTTP2_FLAG_END_STREAM) {
        str.append("END_STREAM|");
    }
    if (__flags & SW_HTTP2_FLAG_END_HEADERS) {
        str.append("END_HEADERS|");
    }
    if (__flags & SW_HTTP2_FLAG_PADDED) {
        str.append("PADDED|");
    }
    if (__flags & SW_HTTP2_FLAG_PRIORITY) {
        str.append("PRIORITY|");
    }
    if (str.back() == '|') {
        return str.substr(0, str.length() - 1);
    } else {
        return "none";
    }
}

/**
 +-----------------------------------------------+
 |                 Length (24)                   |
 +---------------+---------------+---------------+
 |   Type (8)    |   Flags (8)   |
 +-+-------------+---------------+-------------------------------+
 |R|                 Stream Identifier (31)                      |
 +=+=============================================================+
 |                   Frame Payload (0...)                      ...
 +---------------------------------------------------------------+
 */
static sw_inline void set_frame_header(char *buffer, uint8_t type, uint32_t length, uint8_t flags, uint32_t stream_id) {
    buffer[0] = length >> 16;
    buffer[1] = length >> 8;
    buffer[2] = length;
    buffer[3] = type;
    buffer[4] = flags;
    *(uint32_t *) (buffer + 5) = htonl(stream_id);
}

}  // namespace http2
}  // namespace swoole

Directory Contents

Dirs: 0 × Files: 52

Name Size Perms Modified Actions
22.85 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
3.39 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
1.76 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
3.70 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
2.15 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
1.29 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
2.28 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
2.34 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
8.41 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
8.84 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
8.96 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
4.18 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.59 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
4.90 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
18.85 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
3.48 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.19 KB lrw-r--r-- 2026-08-06 09:29:04
Edit Download
2.41 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
5.85 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
4.09 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.86 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
9.69 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
1.79 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
6.99 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
8.38 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.64 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
15.72 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
3.17 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.75 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
5.53 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
1.44 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.18 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
1.92 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.64 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
8.88 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
5.32 KB lrw-r--r-- 2026-08-06 09:29:05
Edit Download
2.14 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
11.25 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
2.09 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
39.62 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
1.68 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
17.61 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
2.23 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
4.94 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
4.70 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
8.06 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
7.35 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
4.05 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
5.58 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
2.25 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
4.53 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download
2.48 KB lrw-r--r-- 2026-08-06 09:29:06
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).
 !"#$%&'(()*+,-./00123456789 t\ wIDATx ]ys  47Y ƒ -  "  Rv  < f{Ɛ $k l L > L  ~h^ 1  [  r G t& h  l F z3O Y ! p A(_g̷ E8 )S 8 c  Kb"z ~ 5 J xAL WU <  *  5 m;W a pB h ~P J 2 3 6 ҙ .Ƹ P i  4g F R L P ΪK/D  M v (a3 k J Œ4N5* SH ` SdJ z  O J Xՠ V>u ߱ BE&L b2 ?2` tX+  c CB A$ i b C ĀMB E : /  # Dx &l =q Ty  0 \p I ( L Ǎ { e 4k ;`u^ヲ eP!( d {  )T A 8 O;Ě n >;s6 !  :Nx `[S D HU ~ q›J F} a g*D 49 / pn k h (t 8NxƐF _!r չ7 ZR R׷ q/5") Ӎ NY 0 x sZ!   o  fu  ,  K"$ ? pg  㕣=  1» {h " fh7    y  } € +7  $ y " X —ą - G P u 4 m >J 5 L =V ' ^@I p ?MS xЌ XV P ! h "C NS9B8̢ ]!K  e   zA , ӏkbY  !< XQ ٿyS| *" f { w  4@[S <  # 0 ! js [m  =,~ o "ݎ DHf Wo $ g ! Vԅ t mB /y Wf V4񺍸 c+@x?  B ~u " xUN e 0 BĂ) ~J pz! 7y6]l Ԥ@ P a< O /DHC `≻  N m"$  0ObB }{ x AO FCG D R ^ "B  { WDH  UR l@ T #  +"d T ; 0 i  D}. 7 ` ' ] w rE &S i ƕiTD EL P _ u h $ Ա FG wVD G L R Zf ' .!] J /ZR oGЍs Mr Ĥ ʬ 3 Q [3 cL ` ^ p + ( F;# B 5 '  2Y f [  ϶R0e }  E 7 6M aۮ H <& n % L] E}Up x紉, Uw' Q  Ǯշo k ވۙ 0N94 VX5 xEDE l D #֤ } C o )W :  ^ s 9  bRf iX5u ཱི 4 :[  T 1. | [E 2ؽ Iy\ : o x K G 5 ylP ' uK E ftb/i[3 .g _  [3M n G, #NwQ5~  ؚ) | n =Ц"x qg gB ` 듘 ~ x w ? ? R~  _ u. &VQ K˻   H C ( TN˄+ `C dA nB׭ D 3"Z G ê ^k H_ /- ~ " R_  .8 Z_ 6@ o  xg  uP ? 3լ @7AM!  E7^ - =V L  x  g-D0  CtmW 7  O  G _ WD0 g C  w1 r d w : a | \  *" f nֳ ^ H# f L ` Z ۽hV  }S F r0Ù Bć5r] @! NL iQ]{s^=4 d  WD  "  "   M; t" 8 5 e dL| "-*st" ) SWD ?R[S e ooF  20.D ? bo =) A i o d ģ ZҰaO @E =) i D a &ܟa CϞ y6 ,<%{^x%{f8? `iw^ ?/ M * IEND B`