Lines Matching refs:req

463 void fcgi_request_set_keep(fcgi_request *req, int new_value)  in fcgi_request_set_keep()  argument
465 req->keep = new_value; in fcgi_request_set_keep()
879 fcgi_request *req = calloc(1, sizeof(fcgi_request)); local
880 req->listen_socket = listen_socket;
881 req->fd = -1;
882 req->id = -1;
898 req->out_pos = req->out_buf;
899 req->hook.on_accept = on_accept ? on_accept : fcgi_hook_dummy;
900 req->hook.on_read = on_read ? on_read : fcgi_hook_dummy;
901 req->hook.on_close = on_close ? on_close : fcgi_hook_dummy;
904 req->tcp = !GetNamedPipeInfo((HANDLE)_get_osfhandle(req->listen_socket), NULL, NULL, NULL, NULL);
907 fcgi_hash_init(&req->env);
909 return req;
912 void fcgi_destroy_request(fcgi_request *req) { argument
913 fcgi_hash_destroy(&req->env);
914 free(req);
917 static inline ssize_t safe_write(fcgi_request *req, const void *buf, size_t count) argument
930 if (!req->tcp) {
933 ret = write(req->fd, ((char*)buf)+n, out_len);
937 ret = send(req->fd, ((char*)buf)+n, out_len, 0);
943 ret = write(req->fd, ((char*)buf)+n, count-n);
954 static inline ssize_t safe_read(fcgi_request *req, const void *buf, size_t count) argument
967 if (!req->tcp) {
970 ret = _read(req->fd, ((char*)buf)+n, in_len);
974 ret = recv(req->fd, ((char*)buf)+n, in_len, 0);
980 ret = read(req->fd, ((char*)buf)+n, count-n);
1011 static int fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *end) argument
1037 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC(p, name_len), (char*)p, name_len, (char*)p + name_len, val…
1043 static int fcgi_read_request(fcgi_request *req) argument
1049 req->keep = 0;
1050 req->ended = 0;
1051 req->in_len = 0;
1052 req->out_hdr = NULL;
1053 req->out_pos = req->out_buf;
1055 if (req->has_env) {
1056 fcgi_hash_clean(&req->env);
1058 req->has_env = 1;
1061 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
1070 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
1083 req->id = (hdr.requestIdB1 << 8) + hdr.requestIdB0;
1088 if (safe_read(req, buf, len+padding) != len+padding) {
1093 req->keep = (b->flags & FCGI_KEEP_CONN);
1095 if (req->keep && req->tcp && !req->nodelay) {
1102 setsockopt(req->fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on));
1103 req->nodelay = 1;
1108 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("…
1111 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("…
1114 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("…
1120 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
1133 if (safe_read(req, buf, len+padding) != len+padding) {
1134 req->keep = 0;
1138 if (!fcgi_get_params(req, buf, buf+len)) {
1139 req->keep = 0;
1143 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
1145 req->keep = 0;
1157 if (safe_read(req, buf, len+padding) != len+padding) {
1158 req->keep = 0;
1162 if (!fcgi_get_params(req, buf, buf+len)) {
1163 req->keep = 0;
1167 q = req->env.list;
1201 if (safe_write(req, buf, sizeof(fcgi_header) + len) != (ssize_t)sizeof(fcgi_header)+len) {
1202 req->keep = 0;
1213 int fcgi_read(fcgi_request *req, char *str, int len) argument
1222 if (req->in_len == 0) {
1223 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) ||
1226 req->keep = 0;
1229 req->in_len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0;
1230 req->in_pad = hdr.paddingLength;
1231 if (req->in_len == 0) {
1236 if (req->in_len >= rest) {
1237 ret = (int)safe_read(req, str, rest);
1239 ret = (int)safe_read(req, str, req->in_len);
1242 req->keep = 0;
1245 req->in_len -= ret;
1249 if (req->in_len == 0) {
1250 if (req->in_pad) {
1251 if (safe_read(req, buf, req->in_pad) != req->in_pad) {
1252 req->keep = 0;
1266 void fcgi_close(fcgi_request *req, int force, int destroy) argument
1268 if (destroy && req->has_env) {
1269 fcgi_hash_clean(&req->env);
1270 req->has_env = 0;
1274 if (is_impersonate && !req->tcp) {
1279 if ((force || !req->keep) && req->fd >= 0) {
1281 if (!req->tcp) {
1282 HANDLE pipe = (HANDLE)_get_osfhandle(req->fd);
1292 shutdown(req->fd, 1);
1294 while (recv(req->fd, buf, sizeof(buf), 0) > 0) {}
1296 closesocket(req->fd);
1302 shutdown(req->fd, 1);
1304 while (recv(req->fd, buf, sizeof(buf), 0) > 0) {}
1306 close(req->fd);
1309 req->nodelay = 0;
1311 req->fd = -1;
1313 req->hook.on_close();
1317 int fcgi_is_closed(fcgi_request *req) argument
1319 return (req->fd < 0);
1360 int fcgi_accept_request(fcgi_request *req) argument
1368 if (req->fd < 0) {
1374 req->hook.on_accept();
1376 if (!req->tcp) {
1377 pipe = (HANDLE)_get_osfhandle(req->listen_socket);
1378 FCGI_LOCK(req->listen_socket);
1386 FCGI_UNLOCK(req->listen_socket);
1394 req->fd = req->listen_socket;
1395 FCGI_UNLOCK(req->listen_socket);
1397 SOCKET listen_socket = (SOCKET)_get_osfhandle(req->listen_socket);
1400 int listen_socket = req->listen_socket;
1405 FCGI_LOCK(req->listen_socket);
1406 req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
1407 FCGI_UNLOCK(req->listen_socket);
1410 if (req->fd >= 0 && !fcgi_is_allowed()) {
1412 closesocket(req->fd);
1413 req->fd = -1;
1419 if (req->fd < 0 && (in_shutdown || errno != EINTR)) {
1421 if (req->fd < 0 && (in_shutdown || (errno != EINTR && errno != ECONNABORTED))) {
1429 if (req->fd >= 0) {
1434 fds.fd = req->fd;
1444 fcgi_close(req, 1, 0);
1446 if (req->fd < FD_SETSIZE) {
1452 FD_SET(req->fd, &set);
1455 ret = select(req->fd + 1, &set, NULL, NULL, &tv) >= 0;
1457 if (ret > 0 && FD_ISSET(req->fd, &set)) {
1460 fcgi_close(req, 1, 0);
1463 fcgi_close(req, 1, 0);
1472 req->hook.on_read();
1473 int read_result = fcgi_read_request(req);
1476 if (is_impersonate && !req->tcp) {
1477 pipe = (HANDLE)_get_osfhandle(req->fd);
1479 fcgi_close(req, 1, 1);
1484 return req->fd;
1486 fcgi_close(req, 1, 1);
1491 static inline fcgi_header* open_packet(fcgi_request *req, fcgi_request_type type) argument
1493 req->out_hdr = (fcgi_header*) req->out_pos;
1494 req->out_hdr->type = type;
1495 req->out_pos += sizeof(fcgi_header);
1496 return req->out_hdr;
1499 static inline void close_packet(fcgi_request *req) argument
1501 if (req->out_hdr) {
1502 int len = (int)(req->out_pos - ((unsigned char*)req->out_hdr + sizeof(fcgi_header)));
1504req->out_pos += fcgi_make_header(req->out_hdr, (fcgi_request_type)req->out_hdr->type, req->id, len…
1505 req->out_hdr = NULL;
1509 int fcgi_flush(fcgi_request *req, int end) argument
1513 close_packet(req);
1515 len = (int)(req->out_pos - req->out_buf);
1518 fcgi_end_request_rec *rec = (fcgi_end_request_rec*)(req->out_pos);
1520 fcgi_make_header(&rec->hdr, FCGI_END_REQUEST, req->id, sizeof(fcgi_end_request));
1529 if (safe_write(req, req->out_buf, len) != len) {
1530 req->keep = 0;
1531 req->out_pos = req->out_buf;
1535 req->out_pos = req->out_buf;
1539 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len) argument
1547 if (req->out_hdr && req->out_hdr->type != type) {
1548 close_packet(req);
1554 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1556 if (!req->out_hdr) {
1558 if (!fcgi_flush(req, 0)) {
1562 open_packet(req, type);
1564 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1566 memcpy(req->out_pos, str, rest);
1567 req->out_pos += rest;
1570 memcpy(req->out_pos, str, limit);
1571 req->out_pos += limit;
1574 if (!fcgi_flush(req, 0)) {
1581 limit = (int)(sizeof(req->out_buf) - (req->out_pos - req->out_buf));
1582 if (!req->out_hdr) {
1588 if (!req->out_hdr) {
1589 open_packet(req, type);
1591 memcpy(req->out_pos, str, len);
1592 req->out_pos += len;
1593 } else if (len - limit < (int)(sizeof(req->out_buf) - sizeof(fcgi_header))) {
1595 if (!req->out_hdr) {
1596 open_packet(req, type);
1598 memcpy(req->out_pos, str, limit);
1599 req->out_pos += limit;
1601 if (!fcgi_flush(req, 0)) {
1605 open_packet(req, type);
1606 memcpy(req->out_pos, str + limit, len - limit);
1607 req->out_pos += len - limit;
1613 close_packet(req);
1615 open_packet(req, type);
1616 fcgi_make_header(req->out_hdr, type, req->id, 0xfff8);
1617 req->out_hdr = NULL;
1618 if (!fcgi_flush(req, 0)) {
1621 if (safe_write(req, str + pos, 0xfff8) != 0xfff8) {
1622 req->keep = 0;
1631 open_packet(req, type);
1632 fcgi_make_header(req->out_hdr, type, req->id, (len - pos) - rest);
1633 req->out_hdr = NULL;
1634 if (!fcgi_flush(req, 0)) {
1637 if (safe_write(req, str + pos, (len - pos) - rest) != (len - pos) - rest) {
1638 req->keep = 0;
1642 open_packet(req, type);
1643 memcpy(req->out_pos, str + len - rest, rest);
1644 req->out_pos += rest;
1651 int fcgi_end(fcgi_request *req) { argument
1653 if (!req->ended) {
1654 ret = fcgi_flush(req, 1);
1655 req->ended = 1;
1660 int fcgi_finish_request(fcgi_request *req, int force_close) argument
1664 if (req->fd >= 0) {
1665 ret = fcgi_end(req);
1666 fcgi_close(req, force_close, 1);
1671 int fcgi_has_env(fcgi_request *req) argument
1673 return req && req->has_env;
1676 char* fcgi_getenv(fcgi_request *req, const char* var, int var_len) argument
1680 if (!req) return NULL;
1682 return fcgi_hash_get(&req->env, FCGI_HASH_FUNC(var, var_len), (char*)var, var_len, &val_len);
1685 char* fcgi_quick_getenv(fcgi_request *req, const char* var, int var_len, unsigned int hash_value) argument
1689 return fcgi_hash_get(&req->env, hash_value, (char*)var, var_len, &val_len);
1692 char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val) argument
1694 if (!req) return NULL;
1696 fcgi_hash_del(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len);
1699 …return fcgi_hash_set(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len, val, (unsigned int)str…
1703 char* fcgi_quick_putenv(fcgi_request *req, char* var, int var_len, unsigned int hash_value, char* v… argument
1706 fcgi_hash_del(&req->env, hash_value, var, var_len);
1709 return fcgi_hash_set(&req->env, hash_value, var, var_len, val, (unsigned int)strlen(val));
1713 void fcgi_loadenv(fcgi_request *req, fcgi_apply_func func, zval *array) argument
1715 fcgi_hash_apply(&req->env, func, array);