From: Stephen D. <sd...@us...> - 2005-04-27 00:20:30
|
Update of /cvsroot/naviserver/naviserver/nsd In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24570 Modified Files: tclresp.c Log Message: Cleanup and reformat code. Clarify comments. Index: tclresp.c =================================================================== RCS file: /cvsroot/naviserver/naviserver/nsd/tclresp.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** tclresp.c 18 Apr 2005 13:04:14 -0000 1.2 --- tclresp.c 27 Apr 2005 00:20:20 -0000 1.3 *************** *** 1,7 **** /* ! * The contents of this file are subject to the AOLserver Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at ! * http://aolserver.com/. * * Software distributed under the License is distributed on an "AS IS" --- 1,7 ---- /* ! * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at ! * http://mozilla.org/. * * Software distributed under the License is distributed on an "AS IS" *************** *** 32,36 **** * tclresp.c -- * ! * Tcl commands for returning data to the user agent. */ --- 32,36 ---- * tclresp.c -- * ! * Tcl commands for returning data to the user agent. */ *************** *** 39,45 **** --- 39,51 ---- #include "nsd.h" + + /* + * Static functions defined in this file. + */ + static int Result(Tcl_Interp *interp, int result); static int GetConn(ClientData arg, Tcl_Interp *interp, Ns_Conn **connPtr); + /* *************** *** 48,59 **** * NsTclHeadersObjCmd -- * ! * Spit out initial HTTP response; this is for backwards ! * compatibility only. * * Results: ! * Tcl result. * * Side effects: ! * None. * *---------------------------------------------------------------------- --- 54,65 ---- * NsTclHeadersObjCmd -- * ! * Implements ns_headers. Set default response headers and flush ! * all headers to client. * * Results: ! * Tcl result. * * Side effects: ! * None. * *---------------------------------------------------------------------- *************** *** 72,76 **** } if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; } if (Tcl_GetIntFromObj(interp, objv[1], &status) != TCL_OK) { --- 78,82 ---- } if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; } if (Tcl_GetIntFromObj(interp, objv[1], &status) != TCL_OK) { *************** *** 84,87 **** --- 90,94 ---- } Ns_ConnSetRequiredHeaders(conn, type, len); + return Result(interp, Ns_ConnFlushHeaders(conn, status)); } *************** *** 91,103 **** *---------------------------------------------------------------------- * * NsTclReturnObjCmd -- * ! * Implements ns_return as obj command. * * Results: ! * Tcl result. * * Side effects: ! * See docs. * *---------------------------------------------------------------------- --- 98,165 ---- *---------------------------------------------------------------------- * + * NsTclWriteObjCmd -- + * + * Implements ns_write. Send string directly to client. + * + * Results: + * Tcl result. + * + * Side effects: + * String may be transcoded. + * + *---------------------------------------------------------------------- + */ + + int + NsTclWriteObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) + { + Ns_Conn *conn; + char *bytes; + int length; + int result; + + if (objc != 2) { + Tcl_WrongNumArgs(interp, 1, objv, "string"); + return TCL_ERROR; + } + if (GetConn(arg, interp, &conn) != TCL_OK) { + return TCL_ERROR; + } + + /* + * Treat string as binary unless the WriteEncodedFlag is set + * on the current conn. This flag is manipulated via + * ns_startcontent or ns_conn write_encoded. + */ + + if (Ns_ConnGetWriteEncodedFlag(conn) && + (Ns_ConnGetEncoding(conn) != NULL)) { + + bytes = Tcl_GetStringFromObj(objv[1], &length); + result = Ns_WriteCharConn(conn, bytes, length); + + } else { + + bytes = (char *) Tcl_GetByteArrayFromObj(objv[1], &length); + result = Ns_WriteConn(conn, bytes, length); + } + + return Result(interp, result); + } + + + /* + *---------------------------------------------------------------------- + * * NsTclReturnObjCmd -- * ! * Implements ns_return. Send complete response to client with ! * given string as body. * * Results: ! * Tcl result. * * Side effects: ! * String may be transcoded. Connection will be closed. * *---------------------------------------------------------------------- *************** *** 115,125 **** } if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; } ! if (Tcl_GetIntFromObj(interp, objv[objc-3], &status) != TCL_OK) { return TCL_ERROR; } ! result = Ns_ConnReturnCharData(conn, status, Tcl_GetString(objv[objc-1]), -1, ! Tcl_GetString(objv[objc-2])); return Result(interp, result); } --- 177,188 ---- } if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; } ! if (Tcl_GetIntFromObj(interp, objv[1], &status) != TCL_OK) { return TCL_ERROR; } ! result = Ns_ConnReturnCharData(conn, status, Tcl_GetString(objv[3]), -1, ! Tcl_GetString(objv[2])); ! return Result(interp, result); } *************** *** 131,141 **** * NsTclRespondObjCmd -- * ! * Implements ns_respond as obj command. * * Results: ! * Tcl result. * * Side effects: ! * See ns_respond. * *---------------------------------------------------------------------- --- 194,205 ---- * NsTclRespondObjCmd -- * ! * Implements ns_respond. Send complete response to client using ! * a variety of options. * * Results: ! * Tcl result. * * Side effects: ! * String data may be transcoded. Connection will be closed. * *---------------------------------------------------------------------- *************** *** 150,154 **** char *string = NULL, *filename = NULL, *chanid = NULL; Ns_Set *set = NULL; ! Tcl_Channel chan; int retval; --- 214,218 ---- char *string = NULL, *filename = NULL, *chanid = NULL; Ns_Set *set = NULL; ! Tcl_Channel chan; int retval; *************** *** 179,186 **** if (setid != NULL) { set = Ns_TclGetSet(interp, setid); ! if (set == NULL) { Ns_TclPrintfResult(interp, "illegal ns_set id: \"%s\"", setid); return TCL_ERROR; ! } } if (GetConn(arg, interp, &conn) != TCL_OK) { --- 243,250 ---- if (setid != NULL) { set = Ns_TclGetSet(interp, setid); ! if (set == NULL) { Ns_TclPrintfResult(interp, "illegal ns_set id: \"%s\"", setid); return TCL_ERROR; ! } } if (GetConn(arg, interp, &conn) != TCL_OK) { *************** *** 188,192 **** } if (set != NULL) { ! Ns_ConnReplaceHeaders(conn, set); } --- 252,256 ---- } if (set != NULL) { ! Ns_ConnReplaceHeaders(conn, set); } *************** *** 196,200 **** */ ! if (Ns_TclGetOpenChannel(interp, chanid, 0, 1, &chan) != TCL_OK) { return TCL_ERROR; } --- 260,264 ---- */ ! if (Ns_TclGetOpenChannel(interp, chanid, 0, 1, &chan) != TCL_OK) { return TCL_ERROR; } *************** *** 225,235 **** * NsTclReturnFileObjCmd -- * ! * Return an open file. (ns_returnfile) * * Results: ! * Tcl result. * * Side effects: ! * See docs. * *---------------------------------------------------------------------- --- 289,301 ---- * NsTclReturnFileObjCmd -- * ! * Implements ns_returnfile. Send complete response to client ! * using contents of filename if exists and is readable, otherwise ! * send error response. * * Results: ! * Tcl result. * * Side effects: ! * Fastpath cache may be used. Connection will be closed. * *---------------------------------------------------------------------- *************** *** 253,257 **** } return Result(interp, Ns_ConnReturnFile(conn, status, Tcl_GetString(objv[2]), ! Tcl_GetString(objv[3]))); } --- 319,323 ---- } return Result(interp, Ns_ConnReturnFile(conn, status, Tcl_GetString(objv[2]), ! Tcl_GetString(objv[3]))); } *************** *** 262,272 **** * NsTclReturnFpObjCmd -- * ! * Implements ns_returnfp. (actually accepts any open channel) * * Results: ! * Tcl result. * * Side effects: ! * See docs. * *---------------------------------------------------------------------- --- 328,339 ---- * NsTclReturnFpObjCmd -- * ! * Implements ns_returnfp. Send complete response to client using ! * len bytes from given channel. * * Results: ! * Tcl result. * * Side effects: ! * Will close connection. * *---------------------------------------------------------------------- *************** *** 277,281 **** { int len, status; ! Tcl_Channel chan; Ns_Conn *conn; --- 344,348 ---- { int len, status; ! Tcl_Channel chan; Ns_Conn *conn; *************** *** 293,300 **** return TCL_ERROR; } ! if (Ns_TclGetOpenChannel(interp, Tcl_GetString(objv[3]), 0, 1, &chan) != TCL_OK) { return TCL_ERROR; } ! return Result(interp, Ns_ConnReturnOpenChannel(conn, status, Tcl_GetString(objv[2]), chan, len)); } --- 360,418 ---- return TCL_ERROR; } ! if (Ns_TclGetOpenChannel(interp, Tcl_GetString(objv[3]), 0, 1, &chan) ! != TCL_OK) { return TCL_ERROR; } ! ! return Result(interp, ! Ns_ConnReturnOpenChannel(conn, status, Tcl_GetString(objv[2]), chan, len)); ! } ! ! ! /* ! *---------------------------------------------------------------------- ! * ! * NsTclConnSendFpObjCmd -- ! * ! * Implements ns_connsendfp. Send len bytes from given channel ! * directly to client without sending headers. ! * ! * Results: ! * Tcl result. ! * ! * Side effects: ! * Will close connection. ! * ! *---------------------------------------------------------------------- ! */ ! ! int ! NsTclConnSendFpObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) ! { ! Ns_Conn *conn; ! Tcl_Channel chan; ! int len; ! ! if (objc != 3) { ! Tcl_WrongNumArgs(interp, 1, objv, "channel len"); ! return TCL_ERROR; ! } ! if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; ! } ! if (Ns_TclGetOpenChannel(interp, Tcl_GetString(objv[1]), 0, 1, &chan) ! != TCL_OK) { ! return TCL_ERROR; ! } ! if (Tcl_GetIntFromObj(interp, objv[2], &len) != TCL_OK) { ! return TCL_ERROR; ! } ! if (Ns_ConnSendChannel(conn, chan, len) != NS_OK) { ! Ns_TclPrintfResult(interp, "could not send %d bytes from channel %s", ! len, Tcl_GetString(objv[1])); ! return TCL_ERROR; ! } ! ! return TCL_OK; } *************** *** 305,315 **** * NsTclReturnBadRequestObjCmd -- * ! * Implements ns_returnbadrequest as obj command. * * Results: ! * Tcl result. * * Side effects: ! * See docs. * *---------------------------------------------------------------------- --- 423,434 ---- * NsTclReturnBadRequestObjCmd -- * ! * Implements ns_returnbadrequest. Send an error response to ! * client with HTTP status code 400. * * Results: ! * Tcl result. * * Side effects: ! * Will close connection. * *---------------------------------------------------------------------- *************** *** 328,331 **** --- 447,451 ---- return TCL_ERROR; } + return Result(interp, Ns_ConnReturnBadRequest(conn, Tcl_GetString(objv[1]))); } *************** *** 335,349 **** *---------------------------------------------------------------------- * ! * NsTclSimpleReturnObjCmd -- * ! * A generic way of returning from tcl; this implements ! * ns_returnnotfound and ns_returnforbidden. It uses the ! * clientdata to know what to do. * * Results: ! * Tcl result. * * Side effects: ! * Will call proc that is clientdata. * *---------------------------------------------------------------------- --- 455,468 ---- *---------------------------------------------------------------------- * ! * ReturnObjCmd -- * ! * Implements ns_returnnotfound, ns_returnunauthorized and ! * ns_returnforbidden. Send an error response to client. * * Results: ! * Tcl result. * * Side effects: ! * Will close connection. * *---------------------------------------------------------------------- *************** *** 352,361 **** static int ReturnObjCmd(ClientData arg, Tcl_Interp *interp, int objc, ! Tcl_Obj *CONST objv[], int (*proc) (Ns_Conn *)) { Ns_Conn *conn; if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; } return Result(interp, (*proc)(conn)); --- 471,480 ---- static int ReturnObjCmd(ClientData arg, Tcl_Interp *interp, int objc, ! Tcl_Obj *CONST objv[], int (*proc) (Ns_Conn *)) { Ns_Conn *conn; if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; } return Result(interp, (*proc)(conn)); *************** *** 386,396 **** * NsTclReturnErrorObjCmd -- * ! * Implements ns_returnerror as obj command. * * Results: ! * Tcl result. * * Side effects: ! * See docs. * *---------------------------------------------------------------------- --- 505,516 ---- * NsTclReturnErrorObjCmd -- * ! * Implements ns_returnerror. Send an error response to client ! * with given status code and message. * * Results: ! * Tcl result. * * Side effects: ! * Will close connection. * *---------------------------------------------------------------------- *************** *** 400,405 **** NsTclReturnErrorObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { - int status; Ns_Conn *conn; if (objc != 3) { --- 520,525 ---- NsTclReturnErrorObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { Ns_Conn *conn; + int status; if (objc != 3) { *************** *** 413,419 **** return TCL_ERROR; } return Result(interp, ! Ns_ConnReturnAdminNotice(conn, status, "Request Error", ! Tcl_GetString(objv[2]))); } --- 533,540 ---- return TCL_ERROR; } + return Result(interp, ! Ns_ConnReturnNotice(conn, status, "Request Error", ! Tcl_GetString(objv[2]))); } *************** *** 424,434 **** * NsTclReturnNoticeObjCmd -- * ! * Implements ns_returnnotice command. * * Results: ! * Tcl result. * * Side effects: ! * See docs. * *---------------------------------------------------------------------- --- 545,556 ---- * NsTclReturnNoticeObjCmd -- * ! * Implements ns_returnnotice command. Send a response to client ! * with given status code, title and message. * * Results: ! * Tcl result. * * Side effects: ! * Will close connection. * *---------------------------------------------------------------------- *************** *** 462,472 **** * NsTclReturnRedirectObjCmd -- * ! * Implements ns_returnredirect as obj command. * * Results: ! * Tcl result. * * Side effects: ! * See docs. * *---------------------------------------------------------------------- --- 584,594 ---- * NsTclReturnRedirectObjCmd -- * ! * Implements ns_returnredirect. * * Results: ! * Tcl result. * * Side effects: ! * See Ns_ConnReturnRedirect(). * *---------------------------------------------------------------------- *************** *** 485,590 **** return TCL_ERROR; } - return Result(interp, Ns_ConnReturnRedirect(conn, Tcl_GetString(objv[1]))); - } - - - /* - *---------------------------------------------------------------------- - * - * NsTclWriteObjCmd -- - * - * Implements ns_write as obj command. - * - * Results: - * Tcl result. - * - * Side effects: - * See docs. - * - *---------------------------------------------------------------------- - */ - - int - NsTclWriteObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) - { - Ns_Conn *conn; - char *bytes; - int length; - int result; - - if (objc != 2) { - Tcl_WrongNumArgs(interp, 1, objv, "string"); - return TCL_ERROR; - } - if (GetConn(arg, interp, &conn) != TCL_OK) { - return TCL_ERROR; - } - - /* - * ns_write will treat data it is given as binary, until - * it is specifically given permission to do otherwise through - * the WriteEncodedFlag on the current conn. This flag is - * manipulated via ns_startcontent or ns_conn write_encoded - */ - if (Ns_ConnGetWriteEncodedFlag(conn) && - (Ns_ConnGetEncoding(conn) != NULL)) { ! bytes = Tcl_GetStringFromObj(objv[1], &length); ! result = Ns_WriteCharConn(conn, bytes, length); ! ! } else { ! ! bytes = (char *) Tcl_GetByteArrayFromObj(objv[1], &length); ! result = Ns_WriteConn(conn, bytes, length); ! ! } ! ! return Result(interp, result); ! } ! ! ! /* ! *---------------------------------------------------------------------- ! * ! * NsTclConnSendFpObjCmd -- ! * ! * Implements ns_connsendfp as obj command. ! * ! * Results: ! * Tcl result. ! * ! * Side effects: ! * See docs. ! * ! *---------------------------------------------------------------------- ! */ ! ! int ! NsTclConnSendFpObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) ! { ! Ns_Conn *conn; ! Tcl_Channel chan; ! int len; ! ! if (objc != 3) { ! Tcl_WrongNumArgs(interp, 1, objv, "channel len"); ! return TCL_ERROR; ! } ! if (GetConn(arg, interp, &conn) != TCL_OK) { ! return TCL_ERROR; ! } ! if (Ns_TclGetOpenChannel(interp, Tcl_GetString(objv[1]), 0, 1, &chan) ! != TCL_OK) { ! return TCL_ERROR; ! } ! if (Tcl_GetIntFromObj(interp, objv[2], &len) != TCL_OK) { ! return TCL_ERROR; ! } ! if (Ns_ConnSendChannel(conn, chan, len) != NS_OK) { ! Ns_TclPrintfResult(interp, "could not send %d bytes from channel %s", ! len, Tcl_GetString(objv[1])); ! return TCL_ERROR; ! } ! return TCL_OK; } --- 607,612 ---- return TCL_ERROR; } ! return Result(interp, Ns_ConnReturnRedirect(conn, Tcl_GetString(objv[1]))); } *************** *** 603,610 **** if (itPtr->conn == NULL) { ! Tcl_SetResult(interp, "no connection", TCL_STATIC); ! return TCL_ERROR; } *connPtr = itPtr->conn; return TCL_OK; } --- 625,633 ---- if (itPtr->conn == NULL) { ! Tcl_SetResult(interp, "no connection", TCL_STATIC); ! return TCL_ERROR; } *connPtr = itPtr->conn; + return TCL_OK; } |