Coverage Report

Created: 2026-06-15 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/nghttp2/lib/nghttp2_stream.c
Line
Count
Source
1
/*
2
 * nghttp2 - HTTP/2 C Library
3
 *
4
 * Copyright (c) 2012 Tatsuhiro Tsujikawa
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
#include "nghttp2_stream.h"
26
27
#include <assert.h>
28
29
#include "nghttp2_session.h"
30
#include "nghttp2_helper.h"
31
#include "nghttp2_debug.h"
32
#include "nghttp2_frame.h"
33
34
void nghttp2_stream_init(nghttp2_stream *stream, int32_t stream_id,
35
                         uint8_t flags, nghttp2_stream_state initial_state,
36
                         int32_t remote_initial_window_size,
37
                         int32_t local_initial_window_size,
38
0
                         void *stream_user_data) {
39
0
  *stream = (nghttp2_stream){
40
0
    .state = initial_state,
41
0
    .content_length = -1,
42
0
    .stream_user_data = stream_user_data,
43
0
    .stream_id = stream_id,
44
0
    .remote_window_size = remote_initial_window_size,
45
0
    .local_window_size = local_initial_window_size,
46
0
    .status_code = -1,
47
0
    .http_flags = NGHTTP2_HTTP_FLAG_NONE,
48
0
    .flags = flags,
49
0
    .shut_flags = NGHTTP2_SHUT_NONE,
50
0
    .extpri = NGHTTP2_EXTPRI_DEFAULT_URGENCY,
51
0
    .http_extpri = NGHTTP2_EXTPRI_DEFAULT_URGENCY,
52
0
  };
53
0
}
54
55
0
void nghttp2_stream_free(nghttp2_stream *stream) { (void)stream; }
56
57
0
void nghttp2_stream_shutdown(nghttp2_stream *stream, uint8_t flag) {
58
0
  stream->shut_flags = (uint8_t)(stream->shut_flags | flag);
59
0
}
60
61
void nghttp2_stream_attach_item(nghttp2_stream *stream,
62
0
                                nghttp2_outbound_item *item) {
63
0
  assert((stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL) == 0);
64
0
  assert(stream->item == NULL);
65
66
0
  DEBUGF("stream: stream=%d attach item=%p\n", stream->stream_id, item);
67
68
0
  stream->item = item;
69
0
}
70
71
0
void nghttp2_stream_detach_item(nghttp2_stream *stream) {
72
0
  DEBUGF("stream: stream=%d detach item=%p\n", stream->stream_id, stream->item);
73
74
0
  stream->item = NULL;
75
0
  stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_DEFERRED_ALL);
76
0
}
77
78
0
void nghttp2_stream_defer_item(nghttp2_stream *stream, uint8_t flags) {
79
0
  assert(stream->item);
80
81
0
  DEBUGF("stream: stream=%d defer item=%p cause=%02x\n", stream->stream_id,
82
0
         stream->item, flags);
83
84
0
  stream->flags |= flags;
85
0
}
86
87
void nghttp2_stream_resume_deferred_item(nghttp2_stream *stream,
88
0
                                         uint8_t flags) {
89
0
  assert(stream->item);
90
91
0
  DEBUGF("stream: stream=%d resume item=%p flags=%02x\n", stream->stream_id,
92
0
         stream->item, flags);
93
94
0
  stream->flags = (uint8_t)(stream->flags & ~flags);
95
0
}
96
97
0
int nghttp2_stream_check_deferred_item(nghttp2_stream *stream) {
98
0
  return stream->item && (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_ALL);
99
0
}
100
101
0
int nghttp2_stream_check_deferred_by_flow_control(nghttp2_stream *stream) {
102
0
  return stream->item &&
103
0
         (stream->flags & NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
104
0
}
105
106
static int update_initial_window_size(int32_t *window_size_ptr,
107
                                      int32_t new_initial_window_size,
108
0
                                      int32_t old_initial_window_size) {
109
0
  int64_t new_window_size = (int64_t)(*window_size_ptr) +
110
0
                            new_initial_window_size - old_initial_window_size;
111
0
  if (INT32_MIN > new_window_size ||
112
0
      new_window_size > NGHTTP2_MAX_WINDOW_SIZE) {
113
0
    return -1;
114
0
  }
115
0
  *window_size_ptr = (int32_t)new_window_size;
116
0
  return 0;
117
0
}
118
119
int nghttp2_stream_update_remote_initial_window_size(
120
  nghttp2_stream *stream, int32_t new_initial_window_size,
121
0
  int32_t old_initial_window_size) {
122
0
  return update_initial_window_size(&stream->remote_window_size,
123
0
                                    new_initial_window_size,
124
0
                                    old_initial_window_size);
125
0
}
126
127
int nghttp2_stream_update_local_initial_window_size(
128
  nghttp2_stream *stream, int32_t new_initial_window_size,
129
0
  int32_t old_initial_window_size) {
130
0
  return update_initial_window_size(&stream->local_window_size,
131
0
                                    new_initial_window_size,
132
0
                                    old_initial_window_size);
133
0
}
134
135
0
void nghttp2_stream_promise_fulfilled(nghttp2_stream *stream) {
136
0
  stream->state = NGHTTP2_STREAM_OPENED;
137
0
  stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_PUSH);
138
0
}
139
140
0
nghttp2_stream_proto_state nghttp2_stream_get_state(nghttp2_stream *stream) {
141
0
  if (stream == &nghttp2_stream_root) {
142
0
    return NGHTTP2_STREAM_STATE_IDLE;
143
0
  }
144
145
0
  if (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) {
146
0
    return NGHTTP2_STREAM_STATE_CLOSED;
147
0
  }
148
149
0
  if (stream->flags & NGHTTP2_STREAM_FLAG_PUSH) {
150
0
    if (stream->shut_flags & NGHTTP2_SHUT_RD) {
151
0
      return NGHTTP2_STREAM_STATE_RESERVED_LOCAL;
152
0
    }
153
154
0
    if (stream->shut_flags & NGHTTP2_SHUT_WR) {
155
0
      return NGHTTP2_STREAM_STATE_RESERVED_REMOTE;
156
0
    }
157
0
  }
158
159
0
  if (stream->shut_flags & NGHTTP2_SHUT_RD) {
160
0
    return NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE;
161
0
  }
162
163
0
  if (stream->shut_flags & NGHTTP2_SHUT_WR) {
164
0
    return NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL;
165
0
  }
166
167
0
  if (stream->state == NGHTTP2_STREAM_IDLE) {
168
0
    return NGHTTP2_STREAM_STATE_IDLE;
169
0
  }
170
171
0
  return NGHTTP2_STREAM_STATE_OPEN;
172
0
}
173
174
0
nghttp2_stream *nghttp2_stream_get_parent(nghttp2_stream *stream) {
175
0
  (void)stream;
176
177
0
  return NULL;
178
0
}
179
180
0
nghttp2_stream *nghttp2_stream_get_next_sibling(nghttp2_stream *stream) {
181
0
  (void)stream;
182
183
0
  return NULL;
184
0
}
185
186
0
nghttp2_stream *nghttp2_stream_get_previous_sibling(nghttp2_stream *stream) {
187
0
  (void)stream;
188
189
0
  return NULL;
190
0
}
191
192
0
nghttp2_stream *nghttp2_stream_get_first_child(nghttp2_stream *stream) {
193
0
  (void)stream;
194
195
0
  return NULL;
196
0
}
197
198
0
int32_t nghttp2_stream_get_weight(nghttp2_stream *stream) {
199
0
  (void)stream;
200
201
0
  return NGHTTP2_DEFAULT_WEIGHT;
202
0
}
203
204
0
int32_t nghttp2_stream_get_sum_dependency_weight(nghttp2_stream *stream) {
205
0
  (void)stream;
206
207
0
  return 0;
208
0
}
209
210
0
int32_t nghttp2_stream_get_stream_id(nghttp2_stream *stream) {
211
0
  return stream->stream_id;
212
0
}