void CServerQuery::UpdateInternetList( void ) { HWND hDialogWnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DLG_GRABBINGLIST), CMainWindow::GetMain(), NULL, 0L); ShowWindow( hDialogWnd, SW_SHOW ); UpdateWindow( hDialogWnd ); char* szList; int uiDataLen = http_fetch( "vicecitymultiplayer.com/servers.php", &szList ); if ( uiDataLen == -1 ) { // Try the backup list uiDataLen = http_fetch( "liberty-unleashed.co.uk/vcmp/servers.txt", &szList ); if ( uiDataLen == -1 ) { MessageBox( 0, _TEXT( "Unable to contact master list" ), _TEXT( "Error" ), MB_ICONEXCLAMATION | MB_OK ); return; } } char* oldList = szList; char* context = NULL; // Have to use strtok_r here since we use strtok in the Query, and it screws things over // Could be done better but this isnt the most amazing code ever szList = strtok_s( szList, "\n", &context ); while ( szList ) { unsigned short usPort = 0; char szIP[ 16 ] = { 0 }; SplitIPAndPort( szList, szIP, usPort ); if ( usPort ) { CServers* pServer = CServerManager::Find( szIP, usPort ); if ( !pServer ) { pServer = CServerManager::New(); if ( pServer ) { pServer->SetServerIP( szIP ); pServer->SetServerPort( usPort ); } } if ( pServer ) { if ( !(pServer->GetType() & E_ST_INTERNET) ) pServer->SetType( (E_SERVER_TYPE)(pServer->GetType() | E_ST_INTERNET) ); pServer->Query(); } } szList = strtok_s( 0, "\n", &context ); } free( oldList ); DestroyWindow(hDialogWnd); }
void CServerQuery::UpdateOfficialList( void ) { HWND hDialogWnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DLG_GRABBINGLIST), CMainWindow::GetMain(), NULL, 0L); ShowWindow( hDialogWnd, SW_SHOW ); UpdateWindow( hDialogWnd ); char* szList; int uiDataLen = http_fetch( "vicecitymultiplayer.com/official.txt", &szList ); if ( uiDataLen == -1 ) { uiDataLen = http_fetch( "liberty-unleashed.co.uk/vcmp/official.txt", &szList ); if ( uiDataLen == -1 ) { MessageBox( 0, _TEXT( "Unable to contact master list" ), _TEXT( "Error" ), MB_ICONEXCLAMATION | MB_OK ); return; } } char* oldList = szList; char* context = NULL; szList = strtok_s( szList, "\n", &context ); while ( szList ) { unsigned short usPort = 0; char szIP[ 16 ] = { 0 }; SplitIPAndPort( szList, szIP, usPort ); if ( usPort ) { CServers* pServer = CServerManager::Find( szIP, usPort ); if ( !pServer ) { pServer = CServerManager::New(); if ( pServer ) { pServer->SetServerIP( szIP ); pServer->SetServerPort( usPort ); } } if ( pServer ) { if ( !(pServer->GetType() & E_ST_OFFICIAL) ) pServer->SetType( (E_SERVER_TYPE)(pServer->GetType() | E_ST_OFFICIAL) ); pServer->Query(); } } szList = strtok_s( 0, "\n", &context ); } free( oldList ); DestroyWindow(hDialogWnd); }
static void * http_worker(void *arg) { struct http_worker_description *hwdp; int ret; if (threaded) { ret = pthread_barrier_wait(&statep->start_barrier); if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) err(-1, "pthread_barrier_wait"); } else { signal_barrier_wait(); } hwdp = arg; while (!statep->run_done) { if (http_fetch(&statep->sin, statep->path, QUIET) < 0) { hwdp->hwd_errorcount++; continue; } /* Don't count transfers that didn't finish in time. */ if (!statep->run_done) hwdp->hwd_count++; } if (threaded) return (NULL); else exit(0); }
int main(int argc, char *argv[]) { int r, ret; short statuscode; char *fileBuf; /* Pointer to downloaded data */ ret = http_fetch(argv[1], &fileBuf, &statuscode); /* Downloads page */ if (ret == -1) /* All HTTP Fetcher functions return */ http_perror("http_fetch"); /* -1 on error. */ else { printf("Page successfully downloaded(%d). (%s)\n", ret, argv[1]); printf("%s\n", fileBuf); printf("-----------------%d-----------------\n", ret); free(fileBuf); } return 0; }
/* Download a file from an HTTP server and put in folder - uses http_fetcher */ bool downloadFile(char* ip, char* port, char* serverFile, char* localFolder) { /* Build URL */ int urlLen = strlen("http://") + strlen(ip) + strlen(":") + strlen(port) + strlen("/") + strlen(serverFile) + 1; char *url = (char*) malloc(urlLen); snprintf(url, urlLen, "http://%s:%s/%s", ip, port, serverFile); /* Set the user-agent to Internet Explorer 9 :) */ http_setUserAgent("Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))"); /* Download the file */ char *response; int responseLen = http_fetch(url, &response); if (responseLen < 0) return false; /* Build destination */ int destLen = strlen(localFolder) + strlen("/") + strlen(serverFile) + 1; char *dest = (char*) malloc(destLen); snprintf(dest, destLen, "%s/%s", localFolder, serverFile); /* Write to file */ FILE* fp; fp = fopen(dest, "w"); fwrite(response, 1, responseLen, fp); fclose(fp); /* Check file size */ fp = fopen(dest, "r"); fseek(fp, 0L, SEEK_END); int fileSize = ftell(fp); fclose(fp); /* Sanity check */ if (responseLen == fileSize) return true; else return false; }
bool check_url(const char* filename) { char* fileBuf = 0; // Tries to open as an URL for a local file if (strstr(filename, "file://") != 0) { // Tries to open as a regular file after removing 'file://' FILE* f = fopen(&filename[7], "r"); if (f) { fclose(f); return true; } else { stringstream error; error << "ERROR : cannot open file '" << filename << "' : " << http_strerror() << "; for help type \"faust --help\"" << endl; throw faustexception(error.str()); } // Tries to open as a http URL } else if (strstr(filename, "http://") != 0) { if (http_fetch(filename, &fileBuf) != -1) { return true; } else { stringstream error; error << "ERROR : unable to access URL '" << filename << "' : " << http_strerror() << "; for help type \"faust --help\"" << endl; throw faustexception(error.str()); } } else { // Otherwise tries to open as a regular file FILE* f = fopen(filename, "r"); if (f) { fclose(f); return true; } else { stringstream error; error << "ERROR : cannot open file '" << filename << "' : " << strerror(errno) << "; for help type \"faust --help\"" << endl; throw faustexception(error.str()); } } }
int main(int argc, char *argv[]) { int ret; char *url = "www.google.com"; /* Pointer to the url you want */ char *fileBuf; /* Pointer to downloaded data */ ret = http_fetch(url, &fileBuf); /* Downloads page */ if(ret == -1) /* All HTTP Fetcher functions return */ http_perror("http_fetch"); /* -1 on error. */ else printf("Page successfully downloaded. (%s)\n", url); /* * Now we have the page downloaded and stored in fileBuf, we could save it * to disk, print it out, etc. Notice that http_fetch() is the primary * function of the library, and for a barebones request is the only * function you need (in conjunction with http_perror). With HTTP Fetcher * you can also set User Agent, referer, and timeout values, parse * filenames, etc... see the header file (http_fetcher.h) for function * usage/information. */ return 0; }
/* * extract the given (expanded) URL "url" to the given directory "dir" * return -1 on error, 0 else; */ int unpackURL(const char *url, const char *dir) { char *pkg; int rc; char base[MaxPathSize]; char pkg_path[MaxPathSize]; { /* Verify if the URL is really ok */ char expnd[MaxPathSize]; rc=expandURL(expnd, url); if (rc == -1) { warnx("unpackURL: verification expandURL failed"); return -1; } if (strcmp(expnd, url) != 0) { warnx("unpackURL: verification expandURL failed, '%s'!='%s'", expnd, url); return -1; } } pkg=strrchr(url, '/'); if (pkg == NULL) { warnx("unpackURL: no '/' in URL %s?!", url); return -1; } (void) snprintf(base, sizeof(base), "%.*s/", (int)(pkg-url), url); (void) snprintf(pkg_path, sizeof(pkg_path), "%.*s", (int)(pkg-url), url); /* no trailing '/' */ pkg++; /* Leave a hint for any depending pkgs that may need it */ if (getenv("PKG_PATH") == NULL) { setenv("PKG_PATH", pkg_path, 1); #if 0 path_create(pkg_path); /* XXX */ #endif if (Verbose) printf("setenv PKG_PATH='%s'\n", pkg_path); } if (strncmp(url, "http://", 7) == 0) return http_fetch(url, dir); rc = ftp_start(base); if (rc == -1) { warnx("ftp_start() failed"); return -1; /* error */ } { char cmd[1024]; const char *decompress_cmd = NULL; const char *suf; if (Verbose) printf("unpackURL '%s' to '%s'\n", url, dir); suf = suffix_of(pkg); if (!strcmp(suf, "tbz") || !strcmp(suf, "bz2")) decompress_cmd = BZIP2_CMD; else if (!strcmp(suf, "tgz") || !strcmp(suf, "gz")) decompress_cmd = GZIP_CMD; else if (!strcmp(suf, "tar")) ; /* do nothing */ else errx(EXIT_FAILURE, "don't know how to decompress %s, sorry", pkg); /* yes, this is gross, but needed for borken ftp(1) */ (void) snprintf(cmd, sizeof(cmd), "get %s \"| ( cd %s; " TAR_CMD " %s %s -vvxp -f - | tee %s )\"\n", pkg, dir, decompress_cmd != NULL ? "--use-compress-program" : "", decompress_cmd != NULL ? decompress_cmd : "", Verbose ? "/dev/stderr" : "/dev/null"); rc = ftp_cmd(cmd, "\n(226|550).*\n"); if (rc != 226) { warnx("Cannot fetch file (%d!=226)!", rc); return -1; } } return 0; }
int main(int argc, char *argv[]) { int ch, error, i; char *pagebuffer; uintmax_t total; size_t len; pid_t pid; numthreads = DEFAULTTHREADS; numseconds = DEFAULTSECONDS; while ((ch = getopt(argc, argv, "n:s:t")) != -1) { switch (ch) { case 'n': numthreads = atoi(optarg); break; case 's': numseconds = atoi(optarg); break; case 't': threaded = 1; break; default: usage(); } } argc -= optind; argv += optind; if (argc != 3) usage(); if (numthreads > MAXTHREADS) errx(-1, "%d exceeds max threads %d", numthreads, MAXTHREADS); len = roundup(sizeof(struct state), getpagesize()); pagebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); if (pagebuffer == MAP_FAILED) err(-1, "mmap"); if (minherit(pagebuffer, len, INHERIT_SHARE) < 0) err(-1, "minherit"); statep = (struct state *)pagebuffer; bzero(&statep->sin, sizeof(statep->sin)); statep->sin.sin_len = sizeof(statep->sin); statep->sin.sin_family = AF_INET; statep->sin.sin_addr.s_addr = inet_addr(argv[0]); statep->sin.sin_port = htons(atoi(argv[1])); statep->path = argv[2]; /* * Do one test retrieve so we can report the error from it, if any. */ if (http_fetch(&statep->sin, statep->path, 0) < 0) exit(-1); if (threaded) { if (pthread_barrier_init(&statep->start_barrier, NULL, numthreads) != 0) err(-1, "pthread_barrier_init"); } for (i = 0; i < numthreads; i++) { statep->hwd[i].hwd_count = 0; if (threaded) { if (pthread_create(&statep->hwd[i].hwd_thread, NULL, http_worker, &statep->hwd[i]) != 0) err(-1, "pthread_create"); } else { curthread = i; pid = fork(); if (pid < 0) { error = errno; killall(); errno = error; err(-1, "fork"); } if (pid == 0) { http_worker(&statep->hwd[i]); printf("Doh\n"); exit(0); } statep->hwd[i].hwd_pid = pid; } } if (!threaded) { signal(SIGHUP, main_sighup); sleep(2); signal_barrier_wakeup(); } sleep(numseconds); statep->run_done = 1; if (!threaded) sleep(2); for (i = 0; i < numthreads; i++) { if (threaded) { if (pthread_join(statep->hwd[i].hwd_thread, NULL) != 0) err(-1, "pthread_join"); } else { pid = waitpid(statep->hwd[i].hwd_pid, NULL, 0); if (pid == statep->hwd[i].hwd_pid) statep->hwd[i].hwd_pid = 0; } } if (!threaded) killall(); total = 0; for (i = 0; i < numthreads; i++) total += statep->hwd[i].hwd_count; printf("%ju transfers/second\n", total / numseconds); total = 0; for (i = 0; i < numthreads; i++) total += statep->hwd[i].hwd_errorcount; printf("%ju errors/second\n", total / numseconds); return (0); }