int http_put_demo(char* putData) { HTTPParameters httpParams; extern const char HTTP_PUT[]; memset(&httpParams, 0, sizeof(HTTPParameters)); httpParams.Uri = (CHAR*)tls_mem_alloc(128); if(httpParams.Uri == NULL) { printf("malloc error.\n"); return WM_FAILED; } memset(httpParams.Uri, 0, 128); sprintf(httpParams.Uri, "http://%d.%d.%d.%d:8080/TestWeb/login_put.do", RemoteIp[0],RemoteIp[1],RemoteIp[2],RemoteIp[3]); printf("Location: %s\n",httpParams.Uri); httpParams.Verbose = TRUE; http_put(httpParams, putData + strlen(HTTP_PUT)); tls_mem_free(httpParams.Uri); return WM_SUCCESS; }
//! //! Main entry point of the application //! //! @param[in] argc the number of parameter passed on the command line //! @param[in] argv the list of arguments //! //! @return EUCA_OK //! int main(int argc, char *argv[]) { int ch = 0; int result = 0; int tmp_fd = -1; char *tmp_name = NULL; char *command = DEFAULT_COMMAND; char *hostport = NULL; char *manifest = NULL; char *file_name = NULL; char *url = NULL; char *login = NULL; char *password = NULL; char request[STRSIZE] = { 0 }; boolean do_compress = FALSE; boolean do_get = FALSE; while ((ch = getopt(argc, argv, "dh:m:f:zu:l:p:")) != -1) { switch (ch) { case 'h': hostport = optarg; break; case 'm': manifest = optarg; break; case 'd': debug = TRUE; break; case 'f': file_name = optarg; break; case 'u': url = optarg; break; case 'l': login = optarg; break; case 'p': password = optarg; break; case 'z': do_compress = TRUE; break; case '?': default: USAGE(); break; } } argc -= optind; argv += optind; if (argc > 0) { command = argv[0]; } if (strcmp(command, "GetDecryptedImage") == 0 || strcmp(command, "GetObject") == 0) { if (manifest == NULL) { fprintf(stderr, "Error: manifest must be specified\n"); USAGE(); } do_get = TRUE; } else if (strcmp(command, "HttpPut") == 0) { if (url == NULL || file_name == NULL) { fprintf(stderr, "Error: URL and input file must be specified\n"); USAGE(); } do_get = FALSE; } else { fprintf(stderr, "Error: unknown command [%s]\n", command); USAGE(); } if (do_get) { /* use a temporary file for network data */ tmp_name = strdup("walrus-download-XXXXXX"); tmp_fd = safe_mkstemp(tmp_name); if (tmp_fd < 0) { fprintf(stderr, "Error: failed to create a temporary file\n"); USAGE(); } close(tmp_fd); if (hostport) { snprintf(request, STRSIZE, "http://%s%s/%s", hostport, WALRUS_ENDPOINT, manifest); if (strcmp(command, "GetObject") == 0) { result = walrus_object_by_url(request, tmp_name, do_compress); } else { result = walrus_image_by_manifest_url(request, tmp_name, do_compress); } } else { euca_strncpy(request, manifest, STRSIZE); if (strcmp(command, "GetObject") == 0) { result = walrus_object_by_path(request, tmp_name, do_compress); } else { result = walrus_image_by_manifest_path(request, tmp_name, do_compress); } } if (result) { /* error has occured */ cat(tmp_name); fprintf(stderr, "\n"); /* in case error doesn't end with a newline */ remove(tmp_name); } else { /* all's well */ if (file_name) { rename(tmp_name, file_name); } else { fprintf(stderr, "Saved output in %s\n", tmp_name); } } EUCA_FREE(tmp_name); } else { // HttpPut result = http_put(file_name, url, login, password); } return (EUCA_OK); }
int main(int argc,char* argv[]) { int ret,lg,blocksize,r,i; char typebuf[70]; char *data=NULL,*filename=NULL,*proxy=NULL; int data_len = 0; char *type = NULL; enum { ERR, DOPUT, DOGET, DODEL, DOHEA, DOPOST } todo=ERR; if (argc!=3) { fprintf(stderr,"usage: http <cmd> <url>\n\tby <*****@*****.**>\n"); return 1; } i=1; if (!strcasecmp(argv[i],"put")) { todo=DOPUT; } else if (!strcasecmp(argv[i],"get")) { todo=DOGET; } else if (!strcasecmp(argv[i],"delete")) { todo=DODEL; } else if (!strcasecmp(argv[i],"head")) { todo=DOHEA; } else if (!strcasecmp(argv[i],"post")) { todo=DOPOST; } if (todo==ERR) { fprintf(stderr, "Invalid <cmd> '%s',\nmust be " "'put', 'get', 'post', 'delete', or 'head'\n", argv[i] ); return 2; } i++; if ((proxy=getenv("http_proxy"))) { ret=http_proxy_url(proxy); if (ret<0) { return ret; } } ret=http_parse_url(argv[i],&filename); if (ret<0) { return ret; } switch (todo) { /* *** PUT *** */ case DOPUT: fprintf(stderr,"reading stdin...\n"); /* read stdin into memory */ blocksize=16384; lg=0; if (!(data=malloc(blocksize))) { return 3; } while (1) { r=read(0,data+lg,blocksize-lg); if (r<=0) break; lg+=r; if ((3*lg/2)>blocksize) { blocksize *= 4; fprintf(stderr, "read to date: %9d bytes, reallocating buffer to %9d\n", lg,blocksize); if (!(data=realloc(data,blocksize))) { return 4; } } } fprintf(stderr,"read %d bytes\n",lg); ret=http_put(filename,data,lg,0,NULL); fprintf(stderr,"res=%d\n",ret); break; /* *** GET *** */ case DOGET: ret=http_get(filename,&data,&lg,typebuf); fprintf(stderr,"res=%d,type='%s',lg=%d\n",ret,typebuf,lg); fwrite(data,lg,1,stdout); fprintf(stderr, "%s\n", data); break; /* *** HEAD *** */ case DOHEA: ret=http_head(filename,&lg,typebuf); fprintf(stderr,"res=%d,type='%s',lg=%d\n",ret,typebuf,lg); break; /* *** DELETE *** */ case DODEL: ret=http_delete(filename); fprintf(stderr,"res=%d\n",ret); break; case DOPOST: ret = http_post(filename, "your_name=1", 11, NULL, &data, &data_len, &type); fprintf(stderr,"res=%d\n",ret); fprintf(stderr,"%s\n", type); fprintf(stderr,"data: %s\n", data); break; /* impossible... */ default: fprintf(stderr,"impossible todo value=%d\n",todo); return 5; } if (type) { free(type); } if (data) { free(data); } free(filename); return ( (ret==201) || (ret==200) ) ? 0 : ret; }