int TclWeb_UploadData()

in src/mod_rivet_ng/TclWebapache.c [880:937]


int TclWeb_UploadData(char *varname, TclWebRequest *req)
{
    Tcl_Obj* result;
    rivet_server_conf *rsc = NULL;

    rsc = RIVET_SERVER_CONF( req->req->server->module_config );
    /* This sucks - we should use the hook, but I want to
       get everything fixed and working first */
    if (rsc->upload_files_to_var)
    {
        Tcl_Channel chan;

        chan = Tcl_OpenFileChannel (req->interp, req->upload->tempname, "r", 0);
        if (chan == NULL) {
            char* tcl_error_msg;
            int error_number = Tcl_GetErrno();

            Tcl_AddErrorInfo(req->interp,"Error opening channel to uploaded data");
            tcl_error_msg = apr_psprintf(req->req->pool,"Error setting channel option '%s': %s",
                                                        Tcl_ErrnoId(), Tcl_ErrnoMsg(error_number));
            Tcl_AddErrorInfo(req->interp,tcl_error_msg);
            return TCL_ERROR;
        }
        if (Tcl_SetChannelOption(req->interp, chan, "-translation", "binary") == TCL_ERROR) {
            char* tcl_error_msg;
            int error_number = Tcl_GetErrno();

            tcl_error_msg = apr_psprintf(req->req->pool,"Error setting channel option '%s': %s",
                                                        Tcl_ErrnoId(), Tcl_ErrnoMsg(error_number));
            Tcl_AddErrorInfo(req->interp,tcl_error_msg);
            return TCL_ERROR;
        }
        if (Tcl_SetChannelOption(req->interp, chan, "-encoding", "binary") == TCL_ERROR) {
            char* tcl_error_msg;
            int error_number = Tcl_GetErrno();

            tcl_error_msg = apr_psprintf(req->req->pool,"Error setting channel option '%s': %s",
                                                        Tcl_ErrnoId(), Tcl_ErrnoMsg(error_number));
            Tcl_AddErrorInfo(req->interp,tcl_error_msg);
            return TCL_ERROR;
        }

        /* Put data in a variable  */
        result = Tcl_NewObj();
        Tcl_ReadChars(chan, result, (int)ApacheUpload_size(req->upload), 0);
        if (Tcl_Close(req->interp, chan) == TCL_ERROR) {
            return TCL_ERROR;
        }

        Tcl_SetObjResult(req->interp, result);
    } else {
        Tcl_AppendResult(req->interp,
                 "RivetServerConf UploadFilesToVar is not set", NULL);
        return TCL_ERROR;
    }

    return TCL_OK;
}