New Ticket     Tickets     Wiki     Browse Source     Timeline     Roadmap     Ticket Reports     Search

Changeset 79520


Ignore:
Timestamp:
06/16/11 09:25:36 (4 years ago)
Author:
derek@…
Message:

Added CurlPostCmd to handle HTTP POST

CurlPostCmd follows the same template as the other curl functions.

It sets CURLOPT_POSTFIELDS to specify that we will POSTing data.

Note that the curl options set in other functions in this module are
also set here.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gsoc11-statistics/base/src/pextlib1.0/curl.c

    r71235 r79520  
    7070int CurlIsNewerCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]); 
    7171int CurlGetSizeCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]); 
     72int CurlPostCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]); 
    7273 
    7374void CurlInit(void); 
     
    898899 
    899900/** 
     901 * curl post postdata url 
     902 * 
     903 * @param interp                current interpreter 
     904 * @param objc                  number of parameters 
     905 * @param objv                  parameters 
     906 */ 
     907int 
     908CurlPostCmd(Tcl_Interp* interp, int objc, Tcl_Obj* CONST objv[]) 
     909{ 
     910        int theResult = TCL_OK; 
     911        CURL* theHandle = NULL; 
     912        FILE* theFile = NULL; 
     913 
     914        do { 
     915                const char* theURL; 
     916                const char* thePostData; 
     917                CURLcode theCurlCode; 
     918 
     919                /* check the number of parameters */ 
     920                if (objc != 4) { 
     921                        Tcl_WrongNumArgs(interp, 1, objv, "postdata url"); 
     922                        theResult = TCL_ERROR; 
     923                        break; 
     924                } 
     925 
     926                /* Retrieve the url - it is the last parameter */ 
     927                theURL = Tcl_GetString(objv[objc - 1]); 
     928 
     929                /* Retrieve the post data - it's before the url */ 
     930                thePostData = Tcl_GetString(objv[objc - 2]); 
     931                /* Open the file (dev/null) */ 
     932                theFile = fopen( "/dev/null", "a" ); 
     933                if (theFile == NULL) { 
     934                        Tcl_SetResult(interp, strerror(errno), TCL_VOLATILE); 
     935                        theResult = TCL_ERROR; 
     936                        break; 
     937                } 
     938 
     939                /* Create the CURL handle */ 
     940                theHandle = curl_easy_init(); 
     941 
     942                /* Setup the handle */ 
     943                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_URL, theURL); 
     944                if (theCurlCode != CURLE_OK) { 
     945                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     946                        break; 
     947                } 
     948 
     949                /* Specify the POST data */ 
     950                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_POSTFIELDS, thePostData); 
     951                if (theCurlCode != CURLE_OK) { 
     952                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     953                        break; 
     954                } 
     955 
     956                /* -L option */ 
     957                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_FOLLOWLOCATION, 1); 
     958                if (theCurlCode != CURLE_OK) { 
     959                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     960                        break; 
     961                } 
     962 
     963                /* --max-redirs option, same default as curl command line */ 
     964                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_MAXREDIRS, 50); 
     965                if (theCurlCode != CURLE_OK) { 
     966                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     967                        break; 
     968                } 
     969 
     970                /* echo any cookies received on a redirect */ 
     971                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_COOKIEJAR, "/dev/null"); 
     972                if (theCurlCode != CURLE_OK) { 
     973                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     974                        break; 
     975                } 
     976 
     977                /* -f option */ 
     978                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_FAILONERROR, 1); 
     979                if (theCurlCode != CURLE_OK) { 
     980                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     981                        break; 
     982                } 
     983 
     984                /* set timeout on connections */ 
     985                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_CONNECTTIMEOUT, _CURL_CONNECTION_TIMEOUT); 
     986                if (theCurlCode != CURLE_OK) { 
     987                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     988                        break; 
     989                } 
     990 
     991                /* set minimum connection speed */ 
     992                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_LOW_SPEED_LIMIT, _CURL_MINIMUM_XFER_SPEED); 
     993                if (theCurlCode != CURLE_OK) { 
     994                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     995                        break; 
     996                } 
     997 
     998                /* set timeout interval for connections < min xfer speed */ 
     999                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_LOW_SPEED_TIME, _CURL_MINIMUM_XFER_TIMEOUT); 
     1000                if (theCurlCode != CURLE_OK) { 
     1001                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     1002                        break; 
     1003                } 
     1004 
     1005                /* write to the file */ 
     1006                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_WRITEDATA, theFile); 
     1007                if (theCurlCode != CURLE_OK) { 
     1008                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     1009                        break; 
     1010                } 
     1011 
     1012                /* skip the header data */ 
     1013                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_HEADER, 0); 
     1014                if (theCurlCode != CURLE_OK) { 
     1015                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     1016                        break; 
     1017                } 
     1018 
     1019                /* skip the body data */ 
     1020                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_NOBODY, 1); 
     1021                if (theCurlCode != CURLE_OK) { 
     1022                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     1023                        break; 
     1024                } 
     1025 
     1026                /* we do not want any progress */ 
     1027                theCurlCode = curl_easy_setopt(theHandle, CURLOPT_NOPROGRESS, 1); 
     1028                if (theCurlCode != CURLE_OK) { 
     1029                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     1030                        break; 
     1031                } 
     1032 
     1033                /* actually perform the POST */ 
     1034                theCurlCode = curl_easy_perform(theHandle); 
     1035                if (theCurlCode != CURLE_OK) { 
     1036                        theResult = SetResultFromCurlErrorCode(interp, theCurlCode); 
     1037                        break; 
     1038                } 
     1039 
     1040                /* close the file */ 
     1041                (void) fclose( theFile ); 
     1042                theFile = NULL; 
     1043 
     1044                /* clean up */ 
     1045                curl_easy_cleanup( theHandle ); 
     1046                theHandle = NULL; 
     1047        } while (0); 
     1048 
     1049        if (theHandle != NULL) { 
     1050                curl_easy_cleanup(theHandle); 
     1051        } 
     1052 
     1053        if (theFile != NULL) { 
     1054                fclose(theFile); 
     1055        } 
     1056 
     1057        return theResult; 
     1058} 
     1059 
     1060/** 
    9001061 * curl command entry point. 
    9011062 * 
     
    9151076                kCurlFetch, 
    9161077                kCurlIsNewer, 
    917                 kCurlGetSize 
     1078                kCurlGetSize, 
     1079                kCurlPost 
    9181080        } EOption; 
    9191081 
    9201082        static const char *options[] = { 
    921                 "fetch", "isnewer", "getsize", NULL 
     1083                "fetch", "isnewer", "getsize", "post", NULL 
    9221084        }; 
    9231085        int theResult = TCL_OK; 
     
    9511113                        theResult = CurlGetSizeCmd(interp, objc, objv); 
    9521114                        break; 
     1115                case kCurlPost: 
     1116                        theResult = CurlPostCmd(interp, objc, objv); 
     1117                        break; 
    9531118                } 
    9541119        } 
Note: See TracChangeset for help on using the changeset viewer.