/* ** Write data to client from CGI script ** ** IN: ** HTTPSRV_CGI_RES_STRUCT* response - CGI response structure used for forming response ** ** OUT: ** none ** ** Return Value: ** uint_32 - Number of bytes written */ uint_32 HTTPSRV_cgi_write(HTTPSRV_CGI_RES_STRUCT* response) { HTTPSRV_SESSION_STRUCT* session = (HTTPSRV_SESSION_STRUCT*) response->ses_handle; uint_32 retval = 0; if (session == NULL) { return(0); } if (session->response.hdrsent == 0) { session->response.status_code = response->status_code; session->response.content_type = response->content_type; session->response.len = response->content_length; /* ** If there is no content length we have to disable keep alive. ** Otherwise we would have to wait for session timeout. */ if (session->response.len == 0) { session->keep_alive = 0; } httpsrv_sendhdr(session, response->content_length, 1); } if ((response->data != NULL) && (response->data_length)) { retval = httpsrv_write(session, response->data, response->data_length); } return(retval); }
/* ** Write data to client from server side include ** ** IN: ** uint32_t ses_handle - session foe writing ** char* data - user data to write ** uint32_t length - size of data in bytes ** ** OUT: ** none ** ** Return Value: ** uint32_t - Number of bytes written */ uint32_t HTTPSRV_ssi_write(uint32_t ses_handle, char* data, uint32_t length) { HTTPSRV_SESSION_STRUCT* session = (HTTPSRV_SESSION_STRUCT*) ses_handle; uint32_t retval = 0; if ((session != NULL) && (data != NULL) && (length)) { retval = httpsrv_write(session, data, length); } return(retval); }
/* ** Send error page to client ** ** IN: ** HTTPSRV_SESSION_STRUCT* session - session used for transmission ** const char* title - title of error page ** const char* text - text displayed on error page ** ** OUT: ** none ** ** Return Value: ** none */ void httpsrv_send_err_page(HTTPSRV_SESSION_STRUCT *session, const char* title, const char* text) { uint32_t length; char* page; length = snprintf(NULL, 0, ERR_PAGE_FORMAT, title, text); length++; page = _mem_alloc(length*sizeof(char)); session->response.content_type = HTTPSRV_CONTENT_TYPE_HTML; if (page != NULL) { snprintf(page, length, ERR_PAGE_FORMAT, title, text); httpsrv_sendhdr(session, strlen(page), 1); httpsrv_write(session, page, strlen(page)); httpsrv_ses_flush(session); _mem_free(page); } else { httpsrv_sendhdr(session, 0, 0); } }
/* ** Write data to client from CGI script ** ** IN: ** HTTPSRV_CGI_RES_STRUCT* response - CGI response structure used for forming response ** ** OUT: ** none ** ** Return Value: ** uint_32 - Number of bytes written */ uint32_t HTTPSRV_cgi_write(HTTPSRV_CGI_RES_STRUCT* response) { HTTPSRV_SESSION_STRUCT* session = (HTTPSRV_SESSION_STRUCT*) response->ses_handle; uint32_t retval = 0; int32_t wrote; if (session == NULL) { return(0); } if (!(session->flags & HTTPSRV_FLAG_HEADER_SENT)) { session->response.status_code = response->status_code; session->response.content_type = response->content_type; session->response.length = response->content_length; if (response->content_length < 0) { session->flags |= HTTPSRV_FLAG_IS_TRANSCODED; } /* * Ignore rest of received data in buffer (set buffer offset to zero). * We do this because otherwise any buffered but unread data would be * part of response (before header) and render it invalid. */ if (session->buffer.offset <= session->request.content_length) { session->request.content_length -= session->buffer.offset; } session->buffer.offset = 0; httpsrv_sendhdr(session, response->content_length, 1); } if (session->flags & HTTPSRV_FLAG_IS_TRANSCODED) { char length_str[sizeof("FFFFFFFF\r\n")] = {0}; /* Write length. */ snprintf(length_str, sizeof(length_str), "%x\r\n", response->data_length); wrote = httpsrv_write(session, length_str, strlen(length_str)); if (wrote < 0) { retval = 0; goto EXIT; } /* Write data. */ wrote = httpsrv_write(session, response->data, response->data_length); if (wrote < 0) { retval = 0; goto EXIT; } retval = wrote; /* Write tail. */ wrote = httpsrv_write(session, "\r\n", strlen("\r\n")); if (wrote < 0) { retval = 0; goto EXIT; } } else if ((response->data != NULL) && (response->data_length > 0)) { retval = httpsrv_write(session, response->data, response->data_length); } EXIT: session->time = RTCS_time_get(); return(retval); }