Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
apr_status_t status = APR_SUCCESS;
const char *err = msg;
ap_assert(session);
if (!err && reason) {
err = nghttp2_strerror(reason);
}
nghttp2_submit_goaway(session->ngh2, NGHTTP2_FLAG_NONE, 0,
reason, (uint8_t*)err, err? strlen(err):0);
status = nghttp2_session_send(session->ngh2);
dispatch_event(session, H2_PROXYS_EV_LOCAL_GOAWAY, reason, err);
return status;
}
static const char *StateNames[] = {
"INIT", /* H2_PROXYS_ST_INIT */
"DONE", /* H2_PROXYS_ST_DONE */
"IDLE", /* H2_PROXYS_ST_IDLE */
"BUSY", /* H2_PROXYS_ST_BUSY */
"WAIT", /* H2_PROXYS_ST_WAIT */
"LSHUTDOWN", /* H2_PROXYS_ST_LOCAL_SHUTDOWN */
"RSHUTDOWN", /* H2_PROXYS_ST_REMOTE_SHUTDOWN */
};
static const char *state_name(h2_proxys_state state)
{
if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) {
return "unknown";
}
return StateNames[state];
}
static int is_accepting_streams(h2_proxy_session *session)
{
switch (session->state) {
case H2_PROXYS_ST_IDLE:
case H2_PROXYS_ST_BUSY:
case H2_PROXYS_ST_WAIT:
return 1;
default:
return 0;
}
}
static void transit(h2_proxy_session *session, const char *action,
h2_proxys_state nstate)
{
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03345)
"h2_proxy_session(%s): transit [%s] -- %s --> [%s]", session->id,
state_name(session->state), action, state_name(nstate));
session->state = nstate;
}
static void ev_init(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_INIT:
if (h2_proxy_ihash_empty(session->streams)) {
transit(session, "init", H2_PROXYS_ST_IDLE);
}
else {
transit(session, "init", H2_PROXYS_ST_BUSY);
}
break;
default:
/* nop */
break;
}
}
static void ev_local_goaway(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
/* already did that? */
break;
case H2_PROXYS_ST_IDLE:
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
/* all done */
transit(session, "local goaway", H2_PROXYS_ST_DONE);
break;
default:
transit(session, "local goaway", H2_PROXYS_ST_LOCAL_SHUTDOWN);
break;
}
}
static void ev_remote_goaway(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
/* already received that? */
break;
case H2_PROXYS_ST_IDLE:
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
/* all done */
transit(session, "remote goaway", H2_PROXYS_ST_DONE);
break;
default:
transit(session, "remote goaway", H2_PROXYS_ST_REMOTE_SHUTDOWN);
break;
}
}
static void ev_conn_error(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_INIT:
case H2_PROXYS_ST_DONE:
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
/* just leave */
transit(session, "conn error", H2_PROXYS_ST_DONE);
break;
default:
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, arg, session->c,
"h2_proxy_session(%s): conn error -> shutdown", session->id);
session_shutdown(session, arg, msg);
break;
}
}
static void ev_proto_error(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_DONE:
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
/* just leave */
transit(session, "proto error", H2_PROXYS_ST_DONE);
break;
default:
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, session->c,
"h2_proxy_session(%s): proto error -> shutdown", session->id);
session_shutdown(session, arg, msg);
break;
}
}
static void ev_conn_timeout(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
transit(session, "conn timeout", H2_PROXYS_ST_DONE);
break;
default:
session_shutdown(session, arg, msg);
transit(session, "conn timeout", H2_PROXYS_ST_DONE);
break;
}
}
static void ev_no_io(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_BUSY:
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
/* nothing for input and output to do. If we remain
* in this state, we go into a tight loop and suck up
* CPU cycles. Ideally, we'd like to do a blocking read, but that
* is not possible if we have scheduled tasks and wait
* for them to produce something. */
if (h2_proxy_ihash_empty(session->streams)) {
if (!is_accepting_streams(session)) {
/* We are no longer accepting new streams and have
* finished processing existing ones. Time to leave. */
session_shutdown(session, arg, msg);
transit(session, "no io", H2_PROXYS_ST_DONE);
}
else {
/* When we have no streams, no task events are possible,
* switch to blocking reads */
transit(session, "no io", H2_PROXYS_ST_IDLE);
}
}
else {
/* Unable to do blocking reads, as we wait on events from
* task processing in other threads. Do a busy wait with
* backoff timer. */
transit(session, "no io", H2_PROXYS_ST_WAIT);
}
break;
default:
/* nop */
break;
}
}
static void ev_stream_submitted(h2_proxy_session *session, int stream_id,
const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_IDLE:
case H2_PROXYS_ST_WAIT:
transit(session, "stream submitted", H2_PROXYS_ST_BUSY);
break;
default:
/* nop */
break;
}
}
static void ev_stream_done(h2_proxy_session *session, int stream_id,
const char *msg)
{
h2_proxy_stream *stream;
stream = nghttp2_session_get_stream_user_data(session->ngh2, stream_id);
if (stream) {
int touched = (stream->data_sent ||
stream_id <= session->last_stream_id);
apr_status_t status = (stream->error_code == 0)? APR_SUCCESS : APR_EINVAL;
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03364)
"h2_proxy_sesssion(%s): stream(%d) closed "
"(touched=%d, error=%d)",
session->id, stream_id, touched, stream->error_code);
if (status != APR_SUCCESS) {
stream->r->status = 500;
}
else if (!stream->data_received) {
apr_bucket *b;
/* if the response had no body, this is the time to flush
* an empty brigade which will also write the resonse
* headers */
h2_proxy_stream_end_headers_out(stream);
stream->data_received = 1;
b = apr_bucket_flush_create(stream->r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(stream->output, b);
b = apr_bucket_eos_create(stream->r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(stream->output, b);
ap_pass_brigade(stream->r->output_filters, stream->output);
}
stream->state = H2_STREAM_ST_CLOSED;
h2_proxy_ihash_remove(session->streams, stream_id);
h2_proxy_iq_remove(session->suspended, stream_id);
if (session->done) {
session->done(session, stream->r, status, touched);
}
}
switch (session->state) {
default:
/* nop */
break;
}
}
static void ev_stream_resumed(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_WAIT:
transit(session, "stream resumed", H2_PROXYS_ST_BUSY);
break;
default:
/* nop */
break;
}
}
static void ev_data_read(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_IDLE:
case H2_PROXYS_ST_WAIT:
transit(session, "data read", H2_PROXYS_ST_BUSY);
break;
default:
/* nop */
break;
}
}
static void ev_ngh2_done(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_DONE:
/* nop */
break;
default:
transit(session, "nghttp2 done", H2_PROXYS_ST_DONE);
break;
}
}
static void ev_pre_close(h2_proxy_session *session, int arg, const char *msg)
{
switch (session->state) {
case H2_PROXYS_ST_DONE:
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
/* nop */
break;
default:
session_shutdown(session, arg, msg);
break;
}
}
static void dispatch_event(h2_proxy_session *session, h2_proxys_event_t ev,
int arg, const char *msg)
{
switch (ev) {
case H2_PROXYS_EV_INIT:
ev_init(session, arg, msg);
break;
case H2_PROXYS_EV_LOCAL_GOAWAY:
ev_local_goaway(session, arg, msg);
break;
case H2_PROXYS_EV_REMOTE_GOAWAY:
ev_remote_goaway(session, arg, msg);
break;
case H2_PROXYS_EV_CONN_ERROR:
ev_conn_error(session, arg, msg);
break;
case H2_PROXYS_EV_PROTO_ERROR:
ev_proto_error(session, arg, msg);
break;
case H2_PROXYS_EV_CONN_TIMEOUT:
ev_conn_timeout(session, arg, msg);
break;
case H2_PROXYS_EV_NO_IO:
ev_no_io(session, arg, msg);
break;
case H2_PROXYS_EV_STREAM_SUBMITTED:
ev_stream_submitted(session, arg, msg);
break;
case H2_PROXYS_EV_STREAM_DONE:
ev_stream_done(session, arg, msg);
break;
case H2_PROXYS_EV_STREAM_RESUMED:
ev_stream_resumed(session, arg, msg);
break;
case H2_PROXYS_EV_DATA_READ:
ev_data_read(session, arg, msg);
break;
case H2_PROXYS_EV_NGH2_DONE:
ev_ngh2_done(session, arg, msg);
break;
case H2_PROXYS_EV_PRE_CLOSE:
ev_pre_close(session, arg, msg);
break;
default:
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, session->c,
"h2_proxy_session(%s): unknown event %d",
session->id, ev);
break;
}
}
static int send_loop(h2_proxy_session *session)
{
while (nghttp2_session_want_write(session->ngh2)) {
int rv = nghttp2_session_send(session->ngh2);
if (rv < 0 && nghttp2_is_fatal(rv)) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
"h2_proxy_session(%s): write, rv=%d", session->id, rv);
dispatch_event(session, H2_PROXYS_EV_CONN_ERROR, rv, NULL);
break;
}
return 1;
}
return 0;
}
apr_status_t h2_proxy_session_process(h2_proxy_session *session)
{
apr_status_t status;
int have_written = 0, have_read = 0;
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
"h2_proxy_session(%s): process", session->id);
run_loop:
switch (session->state) {
case H2_PROXYS_ST_INIT:
status = session_start(session);
if (status == APR_SUCCESS) {
dispatch_event(session, H2_PROXYS_EV_INIT, 0, NULL);
goto run_loop;
}
else {
dispatch_event(session, H2_PROXYS_EV_CONN_ERROR, status, NULL);
}
break;
case H2_PROXYS_ST_BUSY:
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
have_written = send_loop(session);
if (nghttp2_session_want_read(session->ngh2)) {
status = h2_proxy_session_read(session, 0, 0);
if (status == APR_SUCCESS) {
have_read = 1;
}
}
if (!have_written && !have_read
&& !nghttp2_session_want_write(session->ngh2)) {
dispatch_event(session, H2_PROXYS_EV_NO_IO, 0, NULL);
goto run_loop;
}
break;
case H2_PROXYS_ST_WAIT:
if (check_suspended(session) == APR_EAGAIN) {
/* no stream has become resumed. Do a blocking read with
* ever increasing timeouts... */
if (session->wait_timeout < 25) {
session->wait_timeout = 25;
}
else {
session->wait_timeout = H2MIN(apr_time_from_msec(100),
2*session->wait_timeout);
}
status = h2_proxy_session_read(session, 1, session->wait_timeout);
ap_log_cerror(APLOG_MARK, APLOG_TRACE3, status, session->c,
APLOGNO(03365)
"h2_proxy_session(%s): WAIT read, timeout=%fms",
session->id, (float)session->wait_timeout/1000.0);
if (status == APR_SUCCESS) {
have_read = 1;
dispatch_event(session, H2_PROXYS_EV_DATA_READ, 0, NULL);
}
else if (APR_STATUS_IS_TIMEUP(status)
|| APR_STATUS_IS_EAGAIN(status)) {
/* go back to checking all inputs again */
transit(session, "wait cycle", H2_PROXYS_ST_BUSY);
}
}
break;
case H2_PROXYS_ST_IDLE:
break;
case H2_PROXYS_ST_DONE: /* done, session terminated */
return APR_EOF;
default:
ap_log_cerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, session->c,
APLOGNO(03346)"h2_proxy_session(%s): unknown state %d",
session->id, session->state);
dispatch_event(session, H2_PROXYS_EV_PROTO_ERROR, 0, NULL);
break;
}
if (have_read || have_written) {
session->wait_timeout = 0;
}
if (!nghttp2_session_want_read(session->ngh2)
&& !nghttp2_session_want_write(session->ngh2)) {
dispatch_event(session, H2_PROXYS_EV_NGH2_DONE, 0, NULL);
}
return APR_SUCCESS; /* needs to be called again */
}
typedef struct {
h2_proxy_session *session;
h2_proxy_request_done *done;
} cleanup_iter_ctx;
static int cancel_iter(void *udata, void *val)
{
cleanup_iter_ctx *ctx = udata;
h2_proxy_stream *stream = val;
nghttp2_submit_rst_stream(ctx->session->ngh2, NGHTTP2_FLAG_NONE,
stream->id, 0);
return 1;
}
void h2_proxy_session_cancel_all(h2_proxy_session *session)
{
if (!h2_proxy_ihash_empty(session->streams)) {
cleanup_iter_ctx ctx;
ctx.session = session;
ctx.done = session->done;
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03366)
"h2_proxy_session(%s): cancel %d streams",
session->id, (int)h2_proxy_ihash_count(session->streams));
h2_proxy_ihash_iter(session->streams, cancel_iter, &ctx);
session_shutdown(session, 0, NULL);
}
}
static int done_iter(void *udata, void *val)
{
cleanup_iter_ctx *ctx = udata;
h2_proxy_stream *stream = val;
int touched = (stream->data_sent ||
stream->id <= ctx->session->last_stream_id);
ctx->done(ctx->session, stream->r, APR_ECONNABORTED, touched);
return 1;
}
void h2_proxy_session_cleanup(h2_proxy_session *session,
h2_proxy_request_done *done)
{
if (!h2_proxy_ihash_empty(session->streams)) {
cleanup_iter_ctx ctx;
ctx.session = session;
ctx.done = done;
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03519)
"h2_proxy_session(%s): terminated, %d streams unfinished",
session->id, (int)h2_proxy_ihash_count(session->streams));
h2_proxy_ihash_iter(session->streams, done_iter, &ctx);
h2_proxy_ihash_clear(session->streams);
}
}
static int ping_arrived_iter(void *udata, void *val)
{
h2_proxy_stream *stream = val;
if (stream->waiting_on_ping) {
stream->waiting_on_ping = 0;
stream_resume(stream);
}
return 1;
}
static void ping_arrived(h2_proxy_session *session)
{
if (!h2_proxy_ihash_empty(session->streams)) {
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03470)
"h2_proxy_session(%s): ping arrived, unblocking streams",
session->id);
h2_proxy_ihash_iter(session->streams, ping_arrived_iter, &session);
}
}
typedef struct {
h2_proxy_session *session;
conn_rec *c;
apr_off_t bytes;
int updated;
} win_update_ctx;
static int win_update_iter(void *udata, void *val)
{
win_update_ctx *ctx = udata;
h2_proxy_stream *stream = val;
if (stream->r && stream->r->connection == ctx->c) {
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, ctx->session->c,
"h2_proxy_session(%s-%d): win_update %ld bytes",
ctx->session->id, (int)stream->id, (long)ctx->bytes);
nghttp2_session_consume(ctx->session->ngh2, stream->id, ctx->bytes);
ctx->updated = 1;
return 0;
}
return 1;
}
void h2_proxy_session_update_window(h2_proxy_session *session,
conn_rec *c, apr_off_t bytes)
{
if (!h2_proxy_ihash_empty(session->streams)) {
win_update_ctx ctx;
ctx.session = session;
ctx.c = c;
ctx.bytes = bytes;
ctx.updated = 0;
h2_proxy_ihash_iter(session->streams, win_update_iter, &ctx);
if (!ctx.updated) {
/* could not find the stream any more, possibly closed, update
* the connection window at least */
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
"h2_proxy_session(%s): win_update conn %ld bytes",
session->id, (long)bytes);
nghttp2_session_consume_connection(session->ngh2, (size_t)bytes);
}
}
}