Lines Matching refs:req

467 void fcgi_request_set_keep(fcgi_request *req, int new_value)  in fcgi_request_set_keep()  argument
469 req->keep = new_value; in fcgi_request_set_keep()
884 fcgi_request *req = calloc(1, sizeof(fcgi_request)); in fcgi_init_request() local
885 req->listen_socket = listen_socket; in fcgi_init_request()
886 req->fd = -1; in fcgi_init_request()
887 req->id = -1; in fcgi_init_request()
903 req->out_pos = req->out_buf; in fcgi_init_request()
904 req->hook.on_accept = on_accept ? on_accept : fcgi_hook_dummy; in fcgi_init_request()
905 req->hook.on_read = on_read ? on_read : fcgi_hook_dummy; in fcgi_init_request()
906 req->hook.on_close = on_close ? on_close : fcgi_hook_dummy; in fcgi_init_request()
909 req->tcp = !GetNamedPipeInfo((HANDLE)_get_osfhandle(req->listen_socket), NULL, NULL, NULL, NULL); in fcgi_init_request()
912 fcgi_hash_init(&req->env); in fcgi_init_request()
914 return req; in fcgi_init_request()
917 void fcgi_destroy_request(fcgi_request *req) { in fcgi_destroy_request() argument
918 fcgi_hash_destroy(&req->env); in fcgi_destroy_request()
919 free(req); in fcgi_destroy_request()
922 static inline ssize_t safe_write(fcgi_request *req, const void *buf, size_t count) in safe_write() argument
935 if (!req->tcp) { in safe_write()
938 ret = write(req->fd, ((char*)buf)+n, out_len); in safe_write()
942 ret = send(req->fd, ((char*)buf)+n, out_len, 0); in safe_write()
948 ret = write(req->fd, ((char*)buf)+n, count-n); in safe_write()
959 static inline ssize_t safe_read(fcgi_request *req, const void *buf, size_t count) in safe_read() argument
972 if (!req->tcp) { in safe_read()
975 ret = read(req->fd, ((char*)buf)+n, in_len); in safe_read()
979 ret = recv(req->fd, ((char*)buf)+n, in_len, 0); in safe_read()
985 ret = read(req->fd, ((char*)buf)+n, count-n); in safe_read()
1016 static int fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *end) in fcgi_get_params() argument
1042 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC(p, name_len), (char*)p, name_len, (char*)p + name_len, val… in fcgi_get_params()
1048 static int fcgi_read_request(fcgi_request *req) in fcgi_read_request() argument
1054 req->keep = 0; in fcgi_read_request()
1055 req->ended = 0; in fcgi_read_request()
1056 req->in_len = 0; in fcgi_read_request()
1057 req->out_hdr = NULL; in fcgi_read_request()
1058 req->out_pos = req->out_buf; in fcgi_read_request()
1060 if (req->has_env) { in fcgi_read_request()
1061 fcgi_hash_clean(&req->env); in fcgi_read_request()
1063 req->has_env = 1; in fcgi_read_request()
1066 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1075 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1088 req->id = (hdr.requestIdB1 << 8) + hdr.requestIdB0; in fcgi_read_request()
1093 if (safe_read(req, buf, len+padding) != len+padding) { in fcgi_read_request()
1098 req->keep = (b->flags & FCGI_KEEP_CONN); in fcgi_read_request()
1100 if (req->keep && req->tcp && !req->nodelay) { in fcgi_read_request()
1107 setsockopt(req->fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)); in fcgi_read_request()
1108 req->nodelay = 1; in fcgi_read_request()
1113 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("… in fcgi_read_request()
1116 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("… in fcgi_read_request()
1119 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("… in fcgi_read_request()
1125 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1138 if (safe_read(req, buf, len+padding) != len+padding) { in fcgi_read_request()
1139 req->keep = 0; in fcgi_read_request()
1143 if (!fcgi_get_params(req, buf, buf+len)) { in fcgi_read_request()
1144 req->keep = 0; in fcgi_read_request()
1148 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1150 req->keep = 0; in fcgi_read_request()
1162 if (safe_read(req, buf, len+padding) != len+padding) { in fcgi_read_request()
1163 req->keep = 0; in fcgi_read_request()
1167 if (!fcgi_get_params(req, buf, buf+len)) { in fcgi_read_request()
1168 req->keep = 0; in fcgi_read_request()
1172 q = req->env.list; in fcgi_read_request()
1206 if (safe_write(req, buf, sizeof(fcgi_header) + len) != (ssize_t)sizeof(fcgi_header)+len) { in fcgi_read_request()
1207 req->keep = 0; in fcgi_read_request()
1218 int fcgi_read(fcgi_request *req, char *str, int len) in fcgi_read() argument
1227 if (req->in_len == 0) { in fcgi_read()
1228 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read()
1231 req->keep = 0; in fcgi_read()
1234 req->in_len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0; in fcgi_read()
1235 req->in_pad = hdr.paddingLength; in fcgi_read()
1236 if (req->in_len == 0) { in fcgi_read()
1241 if (req->in_len >= rest) { in fcgi_read()
1242 ret = (int)safe_read(req, str, rest); in fcgi_read()
1244 ret = (int)safe_read(req, str, req->in_len); in fcgi_read()
1247 req->keep = 0; in fcgi_read()
1250 req->in_len -= ret; in fcgi_read()
1254 if (req->in_len == 0) { in fcgi_read()
1255 if (req->in_pad) { in fcgi_read()
1256 if (safe_read(req, buf, req->in_pad) != req->in_pad) { in fcgi_read()
1257 req->keep = 0; in fcgi_read()
1271 void fcgi_close(fcgi_request *req, int force, int destroy) in fcgi_close() argument
1273 if (destroy && req->has_env) { in fcgi_close()
1274 fcgi_hash_clean(&req->env); in fcgi_close()
1275 req->has_env = 0; in fcgi_close()
1279 if (is_impersonate && !req->tcp) { in fcgi_close()
1284 if ((force || !req->keep) && req->fd >= 0) { in fcgi_close()
1286 if (!req->tcp) { in fcgi_close()
1287 HANDLE pipe = (HANDLE)_get_osfhandle(req->fd); in fcgi_close()
1297 shutdown(req->fd, 1); in fcgi_close()
1299 while (recv(req->fd, buf, sizeof(buf), 0) > 0) {} in fcgi_close()
1301 closesocket(req->fd); in fcgi_close()
1307 shutdown(req->fd, 1); in fcgi_close()
1309 while (recv(req->fd, buf, sizeof(buf), 0) > 0) {} in fcgi_close()
1311 close(req->fd); in fcgi_close()
1314 req->nodelay = 0; in fcgi_close()
1316 req->fd = -1; in fcgi_close()
1318 req->hook.on_close(); in fcgi_close()
1322 int fcgi_is_closed(fcgi_request *req) in fcgi_is_closed() argument
1324 return (req->fd < 0); in fcgi_is_closed()
1365 int fcgi_accept_request(fcgi_request *req) argument
1373 if (req->fd < 0) {
1379 if (!req->tcp) {
1380 pipe = (HANDLE)_get_osfhandle(req->listen_socket);
1381 FCGI_LOCK(req->listen_socket);
1389 FCGI_UNLOCK(req->listen_socket);
1397 req->fd = req->listen_socket;
1398 FCGI_UNLOCK(req->listen_socket);
1400 SOCKET listen_socket = (SOCKET)_get_osfhandle(req->listen_socket);
1403 int listen_socket = req->listen_socket;
1408 req->hook.on_accept();
1410 FCGI_LOCK(req->listen_socket);
1411 req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
1412 FCGI_UNLOCK(req->listen_socket);
1415 if (req->fd >= 0 && !fcgi_is_allowed()) {
1417 closesocket(req->fd);
1418 req->fd = -1;
1424 if (req->fd < 0 && (in_shutdown || errno != EINTR)) {
1426 if (req->fd < 0 && (in_shutdown || (errno != EINTR && errno != ECONNABORTED))) {
1434 if (req->fd >= 0) {
1439 fds.fd = req->fd;
1449 fcgi_close(req, 1, 0);
1451 if (req->fd < FD_SETSIZE) {
1457 FD_SET(req->fd, &set);
1460 ret = select(req->fd + 1, &set, NULL, NULL, &tv) >= 0;
1462 if (ret > 0 && FD_ISSET(req->fd, &set)) {
1465 fcgi_close(req, 1, 0);
1468 fcgi_close(req, 1, 0);
1477 req->hook.on_read();
1478 if (fcgi_read_request(req)) {
1480 if (is_impersonate && !req->tcp) {
1481 pipe = (HANDLE)_get_osfhandle(req->fd);
1483 fcgi_close(req, 1, 1);
1488 return req->fd;
1490 fcgi_close(req, 1, 1);
1495 static inline fcgi_header* open_packet(fcgi_request *req, fcgi_request_type type) argument
1497 req->out_hdr = (fcgi_header*) req->out_pos;
1498 req->out_hdr->type = type;
1499 req->out_pos += sizeof(fcgi_header);
1500 return req->out_hdr;
1503 static inline void close_packet(fcgi_request *req) argument
1505 if (req->out_hdr) {
1506 int len = (int)(req->out_pos - ((unsigned char*)req->out_hdr + sizeof(fcgi_header)));
1508req->out_pos += fcgi_make_header(req->out_hdr, (fcgi_request_type)req->out_hdr->type, req->id, len…
1509 req->out_hdr = NULL;
1513 int fcgi_flush(fcgi_request *req, int end) argument
1517 close_packet(req);
1519 len = (int)(req->out_pos - req->out_buf);
1522 fcgi_end_request_rec *rec = (fcgi_end_request_rec*)(req->out_pos);
1524 fcgi_make_header(&rec->hdr, FCGI_END_REQUEST, req->id, sizeof(fcgi_end_request));
1533 if (safe_write(req, req->out_buf, len) != len) {
1534 req->keep = 0;
1535 req->out_pos = req->out_buf;
1539 req->out_pos = req->out_buf;
1543 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len) argument
1551 if (req->out_hdr && req->out_hdr->type != type) {
1552 close_packet(req);
1558 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1560 if (!req->out_hdr) {
1562 if (!fcgi_flush(req, 0)) {
1566 open_packet(req, type);
1568 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1570 memcpy(req->out_pos, str, rest);
1571 req->out_pos += rest;
1574 memcpy(req->out_pos, str, limit);
1575 req->out_pos += limit;
1578 if (!fcgi_flush(req, 0)) {
1585 limit = (int)(sizeof(req->out_buf) - (req->out_pos - req->out_buf));
1586 if (!req->out_hdr) {
1592 if (!req->out_hdr) {
1593 open_packet(req, type);
1595 memcpy(req->out_pos, str, len);
1596 req->out_pos += len;
1597 } else if (len - limit < (int)(sizeof(req->out_buf) - sizeof(fcgi_header))) {
1598 if (!req->out_hdr) {
1599 open_packet(req, type);
1602 memcpy(req->out_pos, str, limit);
1603 req->out_pos += limit;
1605 if (!fcgi_flush(req, 0)) {
1609 open_packet(req, type);
1610 memcpy(req->out_pos, str + limit, len - limit);
1611 req->out_pos += len - limit;
1617 close_packet(req);
1619 open_packet(req, type);
1620 fcgi_make_header(req->out_hdr, type, req->id, 0xfff8);
1621 req->out_hdr = NULL;
1622 if (!fcgi_flush(req, 0)) {
1625 if (safe_write(req, str + pos, 0xfff8) != 0xfff8) {
1626 req->keep = 0;
1635 open_packet(req, type);
1636 fcgi_make_header(req->out_hdr, type, req->id, (len - pos) - rest);
1637 req->out_hdr = NULL;
1638 if (!fcgi_flush(req, 0)) {
1641 if (safe_write(req, str + pos, (len - pos) - rest) != (len - pos) - rest) {
1642 req->keep = 0;
1646 open_packet(req, type);
1647 memcpy(req->out_pos, str + len - rest, rest);
1648 req->out_pos += rest;
1655 int fcgi_end(fcgi_request *req) { argument
1657 if (!req->ended) {
1658 ret = fcgi_flush(req, 1);
1659 req->ended = 1;
1664 int fcgi_finish_request(fcgi_request *req, int force_close) argument
1668 if (req->fd >= 0) {
1669 ret = fcgi_end(req);
1670 fcgi_close(req, force_close, 1);
1675 int fcgi_has_env(fcgi_request *req) argument
1677 return req && req->has_env;
1680 char* fcgi_getenv(fcgi_request *req, const char* var, int var_len) argument
1684 if (!req) return NULL;
1686 return fcgi_hash_get(&req->env, FCGI_HASH_FUNC(var, var_len), (char*)var, var_len, &val_len);
1689 char* fcgi_quick_getenv(fcgi_request *req, const char* var, int var_len, unsigned int hash_value) argument
1693 return fcgi_hash_get(&req->env, hash_value, (char*)var, var_len, &val_len);
1696 char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val) argument
1698 if (!req) return NULL;
1700 fcgi_hash_del(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len);
1703 …return fcgi_hash_set(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len, val, (unsigned int)str…
1707 char* fcgi_quick_putenv(fcgi_request *req, char* var, int var_len, unsigned int hash_value, char* v… argument
1710 fcgi_hash_del(&req->env, hash_value, var, var_len);
1713 return fcgi_hash_set(&req->env, hash_value, var, var_len, val, (unsigned int)strlen(val));
1717 void fcgi_loadenv(fcgi_request *req, fcgi_apply_func func, zval *array) argument
1719 fcgi_hash_apply(&req->env, func, array);