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))) {
1427 int fd_attrs = fcntl(req->fd, F_GETFD);
1431 if (0 > fcntl(req->fd, F_SETFD, fd_attrs | FD_CLOEXEC)) {
1439 if (req->fd >= 0) {
1444 fds.fd = req->fd;
1454 fcgi_close(req, 1, 0);
1456 if (req->fd < FD_SETSIZE) {
1462 FD_SET(req->fd, &set);
1465 ret = select(req->fd + 1, &set, NULL, NULL, &tv) >= 0;
1467 if (ret > 0 && FD_ISSET(req->fd, &set)) {
1470 fcgi_close(req, 1, 0);
1473 fcgi_close(req, 1, 0);
1482 req->hook.on_read();
1483 int read_result = fcgi_read_request(req);
1486 if (is_impersonate && !req->tcp) {
1487 pipe = (HANDLE)_get_osfhandle(req->fd);
1489 fcgi_close(req, 1, 1);
1494 return req->fd;
1496 fcgi_close(req, 1, 1);
1501 static inline fcgi_header* open_packet(fcgi_request *req, fcgi_request_type type) argument
1503 req->out_hdr = (fcgi_header*) req->out_pos;
1504 req->out_hdr->type = type;
1505 req->out_pos += sizeof(fcgi_header);
1506 return req->out_hdr;
1509 static inline void close_packet(fcgi_request *req) argument
1511 if (req->out_hdr) {
1512 int len = (int)(req->out_pos - ((unsigned char*)req->out_hdr + sizeof(fcgi_header)));
1514req->out_pos += fcgi_make_header(req->out_hdr, (fcgi_request_type)req->out_hdr->type, req->id, len…
1515 req->out_hdr = NULL;
1519 int fcgi_flush(fcgi_request *req, int end) argument
1523 close_packet(req);
1525 len = (int)(req->out_pos - req->out_buf);
1528 fcgi_end_request_rec *rec = (fcgi_end_request_rec*)(req->out_pos);
1530 fcgi_make_header(&rec->hdr, FCGI_END_REQUEST, req->id, sizeof(fcgi_end_request));
1539 if (safe_write(req, req->out_buf, len) != len) {
1540 req->keep = 0;
1541 req->out_pos = req->out_buf;
1545 req->out_pos = req->out_buf;
1549 int fcgi_write(fcgi_request *req, fcgi_request_type type, const char *str, int len) argument
1557 if (req->out_hdr && req->out_hdr->type != type) {
1558 close_packet(req);
1564 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1566 if (!req->out_hdr) {
1568 if (!fcgi_flush(req, 0)) {
1572 open_packet(req, type);
1574 limit = sizeof(req->out_buf) - (req->out_pos - req->out_buf);
1576 memcpy(req->out_pos, str, rest);
1577 req->out_pos += rest;
1580 memcpy(req->out_pos, str, limit);
1581 req->out_pos += limit;
1584 if (!fcgi_flush(req, 0)) {
1591 limit = (int)(sizeof(req->out_buf) - (req->out_pos - req->out_buf));
1592 if (!req->out_hdr) {
1598 if (!req->out_hdr) {
1599 open_packet(req, type);
1601 memcpy(req->out_pos, str, len);
1602 req->out_pos += len;
1603 } else if (len - limit < (int)(sizeof(req->out_buf) - sizeof(fcgi_header))) {
1605 if (!req->out_hdr) {
1606 open_packet(req, type);
1608 memcpy(req->out_pos, str, limit);
1609 req->out_pos += limit;
1611 if (!fcgi_flush(req, 0)) {
1615 open_packet(req, type);
1616 memcpy(req->out_pos, str + limit, len - limit);
1617 req->out_pos += len - limit;
1623 close_packet(req);
1625 open_packet(req, type);
1626 fcgi_make_header(req->out_hdr, type, req->id, 0xfff8);
1627 req->out_hdr = NULL;
1628 if (!fcgi_flush(req, 0)) {
1631 if (safe_write(req, str + pos, 0xfff8) != 0xfff8) {
1632 req->keep = 0;
1641 open_packet(req, type);
1642 fcgi_make_header(req->out_hdr, type, req->id, (len - pos) - rest);
1643 req->out_hdr = NULL;
1644 if (!fcgi_flush(req, 0)) {
1647 if (safe_write(req, str + pos, (len - pos) - rest) != (len - pos) - rest) {
1648 req->keep = 0;
1652 open_packet(req, type);
1653 memcpy(req->out_pos, str + len - rest, rest);
1654 req->out_pos += rest;
1661 int fcgi_end(fcgi_request *req) { argument
1663 if (!req->ended) {
1664 ret = fcgi_flush(req, 1);
1665 req->ended = 1;
1670 int fcgi_finish_request(fcgi_request *req, int force_close) argument
1674 if (req->fd >= 0) {
1675 ret = fcgi_end(req);
1676 fcgi_close(req, force_close, 1);
1681 int fcgi_has_env(fcgi_request *req) argument
1683 return req && req->has_env;
1686 char* fcgi_getenv(fcgi_request *req, const char* var, int var_len) argument
1690 if (!req) return NULL;
1692 return fcgi_hash_get(&req->env, FCGI_HASH_FUNC(var, var_len), (char*)var, var_len, &val_len);
1695 char* fcgi_quick_getenv(fcgi_request *req, const char* var, int var_len, unsigned int hash_value) argument
1699 return fcgi_hash_get(&req->env, hash_value, (char*)var, var_len, &val_len);
1702 char* fcgi_putenv(fcgi_request *req, char* var, int var_len, char* val) argument
1704 if (!req) return NULL;
1706 fcgi_hash_del(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len);
1709 …return fcgi_hash_set(&req->env, FCGI_HASH_FUNC(var, var_len), var, var_len, val, (unsigned int)str…
1713 char* fcgi_quick_putenv(fcgi_request *req, char* var, int var_len, unsigned int hash_value, char* v… argument
1716 fcgi_hash_del(&req->env, hash_value, var, var_len);
1719 return fcgi_hash_set(&req->env, hash_value, var, var_len, val, (unsigned int)strlen(val));
1723 void fcgi_loadenv(fcgi_request *req, fcgi_apply_func func, zval *array) argument
1725 fcgi_hash_apply(&req->env, func, array);