static int php_ns_sapi_send_headers(sapi_headers_struct *sapi_headers) { if(SG(sapi_headers).send_default_content_type) { Ns_ConnSetRequiredHeaders(NSG(conn), "text/html", 0); } Ns_ConnFlushHeaders(NSG(conn), SG(sapi_headers).http_response_code); return SAPI_HEADER_SENT_SUCCESSFULLY; }
int Ns_ConnFlushDirect(Ns_Conn *conn, char *buf, int len, int stream) { struct iovec iov[4]; int i, nwrote, towrite, hlen, ioc; char hdr[100]; /* * Queue headers if not already sent. */ if (!(conn->flags & NS_CONN_SENTHDRS)) { if (!stream) { hlen = len; } else { if (conn->request->version > 1.0) { conn->flags |= NS_CONN_CHUNK; } hlen = -1; /* NB: Surpress Content-length header. */ } Ns_ConnSetRequiredHeaders(conn, Ns_ConnGetType(conn), hlen); if (conn->flags & NS_CONN_CHUNK) { Ns_ConnCondSetHeaders(conn, "Transfer-Encoding", "chunked"); } Ns_ConnQueueHeaders(conn, Ns_ConnGetStatus(conn)); } /* * Send content on any request other than HEAD. */ ioc = 0; towrite = 0; if (!(conn->flags & NS_CONN_SKIPBODY)) { if (!(conn->flags & NS_CONN_CHUNK)) { /* * Output content without chunking header/trailers. */ iov[ioc].iov_base = buf; iov[ioc++].iov_len = len; } else { if (len > 0) { /* * Output length header followed by content and trailer. */ iov[ioc].iov_base = hdr; iov[ioc++].iov_len = sprintf(hdr, "%x\r\n", len); iov[ioc].iov_base = buf; iov[ioc++].iov_len = len; iov[ioc].iov_base = "\r\n"; iov[ioc++].iov_len = 2; } if (!stream) { /* * Output end-of-content trailer. */ iov[ioc].iov_base = "0\r\n\r\n"; iov[ioc++].iov_len = 5; } } for (i = 0; i < ioc; ++i) { towrite += iov[i].iov_len; } } /* * Write the output buffer and if not streaming, close the * connection. */ nwrote = Ns_ConnSend(conn, iov, ioc); if (nwrote != towrite) { return NS_ERROR; } if (!stream && Ns_ConnClose(conn) != NS_OK) { return NS_ERROR; } return NS_OK; }