Lines Matching refs:req

461 void fcgi_request_set_keep(fcgi_request *req, int new_value)  in fcgi_request_set_keep()  argument
463 req->keep = new_value; in fcgi_request_set_keep()
872 fcgi_request *req = calloc(1, sizeof(fcgi_request)); in fcgi_init_request() local
873 req->listen_socket = listen_socket; in fcgi_init_request()
874 req->fd = -1; in fcgi_init_request()
875 req->id = -1; in fcgi_init_request()
891 req->out_pos = req->out_buf; in fcgi_init_request()
892 req->hook.on_accept = on_accept ? on_accept : fcgi_hook_dummy; in fcgi_init_request()
893 req->hook.on_read = on_read ? on_read : fcgi_hook_dummy; in fcgi_init_request()
894 req->hook.on_close = on_close ? on_close : fcgi_hook_dummy; in fcgi_init_request()
897 req->tcp = !GetNamedPipeInfo((HANDLE)_get_osfhandle(req->listen_socket), NULL, NULL, NULL, NULL); in fcgi_init_request()
900 fcgi_hash_init(&req->env); in fcgi_init_request()
902 return req; in fcgi_init_request()
905 void fcgi_destroy_request(fcgi_request *req) { in fcgi_destroy_request() argument
906 fcgi_hash_destroy(&req->env); in fcgi_destroy_request()
907 free(req); in fcgi_destroy_request()
910 static inline ssize_t safe_write(fcgi_request *req, const void *buf, size_t count) in safe_write() argument
923 if (!req->tcp) { in safe_write()
926 ret = write(req->fd, ((char*)buf)+n, out_len); in safe_write()
930 ret = send(req->fd, ((char*)buf)+n, out_len, 0); in safe_write()
936 ret = write(req->fd, ((char*)buf)+n, count-n); in safe_write()
947 static inline ssize_t safe_read(fcgi_request *req, const void *buf, size_t count) in safe_read() argument
960 if (!req->tcp) { in safe_read()
963 ret = _read(req->fd, ((char*)buf)+n, in_len); in safe_read()
967 ret = recv(req->fd, ((char*)buf)+n, in_len, 0); in safe_read()
973 ret = read(req->fd, ((char*)buf)+n, count-n); in safe_read()
1004 static int fcgi_get_params(fcgi_request *req, unsigned char *p, unsigned char *end) in fcgi_get_params() argument
1030 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC(p, name_len), (char*)p, name_len, (char*)p + name_len, val… in fcgi_get_params()
1036 static int fcgi_read_request(fcgi_request *req) in fcgi_read_request() argument
1042 req->keep = 0; in fcgi_read_request()
1043 req->ended = 0; in fcgi_read_request()
1044 req->in_len = 0; in fcgi_read_request()
1045 req->out_hdr = NULL; in fcgi_read_request()
1046 req->out_pos = req->out_buf; in fcgi_read_request()
1048 if (req->has_env) { in fcgi_read_request()
1049 fcgi_hash_clean(&req->env); in fcgi_read_request()
1051 req->has_env = 1; in fcgi_read_request()
1054 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1063 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1076 req->id = (hdr.requestIdB1 << 8) + hdr.requestIdB0; in fcgi_read_request()
1081 if (safe_read(req, buf, len+padding) != len+padding) { in fcgi_read_request()
1086 req->keep = (b->flags & FCGI_KEEP_CONN); in fcgi_read_request()
1088 if (req->keep && req->tcp && !req->nodelay) { in fcgi_read_request()
1095 setsockopt(req->fd, IPPROTO_TCP, TCP_NODELAY, (char*)&on, sizeof(on)); in fcgi_read_request()
1096 req->nodelay = 1; in fcgi_read_request()
1101 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("… in fcgi_read_request()
1104 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("… in fcgi_read_request()
1107 …fcgi_hash_set(&req->env, FCGI_HASH_FUNC("FCGI_ROLE", sizeof("FCGI_ROLE")-1), "FCGI_ROLE", sizeof("… in fcgi_read_request()
1113 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1126 if (safe_read(req, buf, len+padding) != len+padding) { in fcgi_read_request()
1127 req->keep = 0; in fcgi_read_request()
1131 if (!fcgi_get_params(req, buf, buf+len)) { in fcgi_read_request()
1132 req->keep = 0; in fcgi_read_request()
1136 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read_request()
1138 req->keep = 0; in fcgi_read_request()
1150 if (safe_read(req, buf, len+padding) != len+padding) { in fcgi_read_request()
1151 req->keep = 0; in fcgi_read_request()
1155 if (!fcgi_get_params(req, buf, buf+len)) { in fcgi_read_request()
1156 req->keep = 0; in fcgi_read_request()
1160 q = req->env.list; in fcgi_read_request()
1192 if (safe_write(req, buf, sizeof(fcgi_header) + len) != (ssize_t)sizeof(fcgi_header)+len) { in fcgi_read_request()
1193 req->keep = 0; in fcgi_read_request()
1204 int fcgi_read(fcgi_request *req, char *str, int len) in fcgi_read() argument
1213 if (req->in_len == 0) { in fcgi_read()
1214 if (safe_read(req, &hdr, sizeof(fcgi_header)) != sizeof(fcgi_header) || in fcgi_read()
1217 req->keep = 0; in fcgi_read()
1220 req->in_len = (hdr.contentLengthB1 << 8) | hdr.contentLengthB0; in fcgi_read()
1221 req->in_pad = hdr.paddingLength; in fcgi_read()
1222 if (req->in_len == 0) { in fcgi_read()
1227 if (req->in_len >= rest) { in fcgi_read()
1228 ret = (int)safe_read(req, str, rest); in fcgi_read()
1230 ret = (int)safe_read(req, str, req->in_len); in fcgi_read()
1233 req->keep = 0; in fcgi_read()
1236 req->in_len -= ret; in fcgi_read()
1240 if (req->in_len == 0) { in fcgi_read()
1241 if (req->in_pad) { in fcgi_read()
1242 if (safe_read(req, buf, req->in_pad) != req->in_pad) { in fcgi_read()
1243 req->keep = 0; in fcgi_read()
1257 void fcgi_close(fcgi_request *req, int force, int destroy) in fcgi_close() argument
1259 if (destroy && req->has_env) { in fcgi_close()
1260 fcgi_hash_clean(&req->env); in fcgi_close()
1261 req->has_env = 0; in fcgi_close()
1265 if (is_impersonate && !req->tcp) { in fcgi_close()
1270 if ((force || !req->keep) && req->fd >= 0) { in fcgi_close()
1272 if (!req->tcp) { in fcgi_close()
1273 HANDLE pipe = (HANDLE)_get_osfhandle(req->fd); in fcgi_close()
1283 shutdown(req->fd, 1); in fcgi_close()
1285 while (recv(req->fd, buf, sizeof(buf), 0) > 0) {} in fcgi_close()
1287 closesocket(req->fd); in fcgi_close()
1293 shutdown(req->fd, 1); in fcgi_close()
1295 while (recv(req->fd, buf, sizeof(buf), 0) > 0) {} in fcgi_close()
1297 close(req->fd); in fcgi_close()
1300 req->nodelay = 0; in fcgi_close()
1302 req->fd = -1; in fcgi_close()
1304 req->hook.on_close(); in fcgi_close()
1308 int fcgi_is_closed(fcgi_request *req) in fcgi_is_closed() argument
1310 return (req->fd < 0); in fcgi_is_closed()
1351 int fcgi_accept_request(fcgi_request *req) argument
1359 if (req->fd < 0) {
1365 req->hook.on_accept();
1367 if (!req->tcp) {
1368 pipe = (HANDLE)_get_osfhandle(req->listen_socket);
1369 FCGI_LOCK(req->listen_socket);
1377 FCGI_UNLOCK(req->listen_socket);
1385 req->fd = req->listen_socket;
1386 FCGI_UNLOCK(req->listen_socket);
1388 SOCKET listen_socket = (SOCKET)_get_osfhandle(req->listen_socket);
1391 int listen_socket = req->listen_socket;
1396 FCGI_LOCK(req->listen_socket);
1397 req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
1398 FCGI_UNLOCK(req->listen_socket);
1401 if (req->fd >= 0 && !fcgi_is_allowed()) {
1403 closesocket(req->fd);
1404 req->fd = -1;
1410 if (req->fd < 0 && (in_shutdown || errno != EINTR)) {
1412 if (req->fd < 0 && (in_shutdown || (errno != EINTR && errno != ECONNABORTED))) {
1418 int fd_attrs = fcntl(req->fd, F_GETFD);
1422 if (0 > fcntl(req->fd, F_SETFD, fd_attrs | FD_CLOEXEC)) {
1430 if (req->fd >= 0) {
1435 fds.fd = req->fd;
1445 fcgi_close(req, 1, 0);
1447 if (req->fd < FD_SETSIZE) {
1453 FD_SET(req->fd, &set);
1456 ret = select(req->fd + 1, &set, NULL, NULL, &tv) >= 0;
1458 if (ret > 0 && FD_ISSET(req->fd, &set)) {
1461 fcgi_close(req, 1, 0);
1464 fcgi_close(req, 1, 0);
1473 req->hook.on_read();
1474 int read_result = fcgi_read_request(req);
1477 if (is_impersonate && !req->tcp) {
1478 pipe = (HANDLE)_get_osfhandle(req->fd);
1480 fcgi_close(req, 1, 1);
1485 return req->fd;
1487 fcgi_close(req, 1, 1);
1492 static inline fcgi_header* open_packet(fcgi_request *req, fcgi_request_type type) argument
1494 req->out_hdr = (fcgi_header*) req->out_pos;
1495 req->out_hdr->type = type;
1496 req->out_pos += sizeof(fcgi_header);
1497 return req->out_hdr;
1500 static inline void close_packet(fcgi_request *req) argument
1502 if (req->out_hdr) {
1503 int len = (int)(req->out_pos - ((unsigned char*)req->out_hdr + sizeof(fcgi_header)));
1505req->out_pos += fcgi_make_header(req->out_hdr, (fcgi_request_type)req->out_hdr->type, req->id, len…
1506 req->out_hdr = NULL;
1510 int fcgi_flush(fcgi_request *req, int end) argument
1514 close_packet(req);
1516 len = (int)(req->out_pos - req->out_buf);
1519 fcgi_end_request_rec *rec = (fcgi_end_request_rec*)(req->out_pos);
1521 fcgi_make_header(&rec->hdr, FCGI_END_REQUEST, req->id, sizeof(fcgi_end_request));
1530 if (safe_write(req, req->out_buf, len) != len) {
1531 req->keep = 0;
1532 req->out_pos = req->out_buf;
1536 req->out_pos = req->out_buf;
1540 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len) argument
1548 if (req->out_hdr && req->out_hdr->type != type) {
1549 close_packet(req);
1555 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1557 if (!req->out_hdr) {
1559 if (!fcgi_flush(req, 0)) {
1563 open_packet(req, type);
1565 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1567 memcpy(req->out_pos, str, rest);
1568 req->out_pos += rest;
1571 memcpy(req->out_pos, str, limit);
1572 req->out_pos += limit;
1575 if (!fcgi_flush(req, 0)) {
1582 limit = (int)(sizeof(req->out_buf) - (req->out_pos - req->out_buf));
1583 if (!req->out_hdr) {
1589 if (!req->out_hdr) {
1590 open_packet(req, type);
1592 req->out_pos = zend_mempcpy(req->out_pos, str, 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 req->out_pos = zend_mempcpy(req->out_pos, str, limit);
1600 if (!fcgi_flush(req, 0)) {
1604 open_packet(req, type);
1605 req->out_pos = zend_mempcpy(req->out_pos, str + limit, len - limit);
1611 close_packet(req);
1613 open_packet(req, type);
1614 fcgi_make_header(req->out_hdr, type, req->id, 0xfff8);
1615 req->out_hdr = NULL;
1616 if (!fcgi_flush(req, 0)) {
1619 if (safe_write(req, str + pos, 0xfff8) != 0xfff8) {
1620 req->keep = 0;
1629 open_packet(req, type);
1630 fcgi_make_header(req->out_hdr, type, req->id, (len - pos) - rest);
1631 req->out_hdr = NULL;
1632 if (!fcgi_flush(req, 0)) {
1635 if (safe_write(req, str + pos, (len - pos) - rest) != (len - pos) - rest) {
1636 req->keep = 0;
1640 open_packet(req, type);
1641 req->out_pos = zend_mempcpy(req->out_pos, str + len - rest, rest);
1648 int fcgi_end(fcgi_request *req) { argument
1650 if (!req->ended) {
1651 ret = fcgi_flush(req, 1);
1652 req->ended = 1;
1657 int fcgi_finish_request(fcgi_request *req, int force_close) argument
1661 if (req->fd >= 0) {
1662 ret = fcgi_end(req);
1663 fcgi_close(req, force_close, 1);
1668 int fcgi_has_env(fcgi_request *req) argument
1670 return req && req->has_env;
1673 char* fcgi_getenv(fcgi_request *req, const char* var, int var_len) argument
1677 if (!req) return NULL;
1679 return fcgi_hash_get(&req->env, FCGI_HASH_FUNC(var, var_len), (char*)var, var_len, &val_len);
1682 char* fcgi_quick_getenv(fcgi_request *req, const char* var, int var_len, unsigned int hash_value) argument
1686 return fcgi_hash_get(&req->env, hash_value, (char*)var, var_len, &val_len);
1689 char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val) argument
1691 if (!req) return NULL;
1693 fcgi_hash_del(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len);
1696 …return fcgi_hash_set(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len, val, (unsigned int)str…
1700 char* fcgi_quick_putenv(fcgi_request *req, char* var, int var_len, unsigned int hash_value, char* v… argument
1703 fcgi_hash_del(&req->env, hash_value, var, var_len);
1706 return fcgi_hash_set(&req->env, hash_value, var, var_len, val, (unsigned int)strlen(val));
1710 void fcgi_loadenv(fcgi_request *req, fcgi_apply_func func, zval *array) argument
1712 fcgi_hash_apply(&req->env, func, array);