#include <stdio.h>
#include <winsock2.h>
#include <wininet.h>
#include <sys/types.h>
#include <sw_http.h>
#define SafeLocalFree(x) if(x != NULL) { LocalFree(x); x = NULL; }
char *wget_with_retryes(char *url, DWORD flags, char *content_type, int content_len, char *postdata, int postdatalen, char *username, char *password, int chances){
int retryes;
char *retval;
retval = NULL;
retryes = 0;
while(retval == NULL && retryes < chances){
retval = wget(url, flags, content_type, content_len, postdata, postdatalen, username, password);
if(retval == NULL){
Sleep(1000);
retryes++;
}
}
return retval;
}
char *wget(char *url, DWORD flags, char *content_type, int content_len, char *postdata, int postdatalen, char *username, char *password){
URL_COMPONENTS urlComponents;
HINTERNET handles[3];
DWORD temporals[4];
char *extra_headers;
char *retval;
char *internal_buf;
DWORD internal_len;
DWORD internal_idx;
DWORD internal_mod;
retval = NULL;
extra_headers = NULL;
temporals[0] = 0;
if(flags && WGET_LITTE_FILE){
internal_mod = 5;
}else if(flags && WGET_MEDIUM_FILE){
internal_mod = 10;
}else if(flags && WGET_BIG_FILE){
internal_mod = 20;
}else if(flags && WGET_HUGE_FILE){
internal_mod = 50;
}else{
internal_mod = 10;
}
memset(&urlComponents, 0, sizeof(URL_COMPONENTS));
urlComponents.dwStructSize = sizeof(URL_COMPONENTS);
urlComponents.dwUrlPathLength = 512;
urlComponents.lpszUrlPath = LocalAlloc(LPTR, urlComponents.dwUrlPathLength-1);
urlComponents.dwHostNameLength = 512;
urlComponents.lpszHostName = LocalAlloc(LPTR, urlComponents.dwHostNameLength-1);
if(!urlComponents.lpszUrlPath[0])
urlComponents.lpszUrlPath[0] = '/';
if(!InternetCrackUrl(url, strlen(url), 0, &urlComponents)){
SetLastError(WGET_CRACK_URL);
return NULL;
}
handles[0] = InternetOpen(APP_USER_AGENT, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE);
if(handles[0] == NULL){
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
SetLastError(WGET_INTERNET_OPEN);
return NULL;
}
handles[1] = InternetConnect(handles[0], urlComponents.lpszHostName, urlComponents.nPort, "", "", INTERNET_SERVICE_HTTP, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_PASSIVE, 0);
if(handles[1] == NULL){
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
InternetCloseHandle(handles[0]);
SetLastError(WGET_INTERNET_CONNECT);
return NULL;
}
if(username != NULL){
if(flags & WGET_PROXY_PASS)
InternetSetOption(handles[1], INTERNET_OPTION_PROXY_USERNAME, username, strlen(username)+1);
if(flags & WGET_HTTP_PASS)
InternetSetOption(handles[1], INTERNET_OPTION_USERNAME, username, strlen(username));
}
if(password != NULL){
if(flags & WGET_PROXY_PASS)
InternetSetOption(handles[1], INTERNET_OPTION_PROXY_PASSWORD, password, strlen(password)+1);
if(flags & WGET_HTTP_PASS)
InternetSetOption(handles[1], INTERNET_OPTION_PASSWORD, password, strlen(password));
}
if(flags & WGET_POST){
handles[2] = HttpOpenRequest(handles[1], "POST", urlComponents.lpszUrlPath, NULL, NULL, NULL, INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE, temporals[0]);
}else{
handles[2] = HttpOpenRequest(handles[1], "GET", urlComponents.lpszUrlPath, NULL, NULL, NULL, INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP | INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS | INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE, temporals[0]);
}
if(handles[2] == NULL){
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
InternetCloseHandle(handles[1]);
InternetCloseHandle(handles[0]);
SetLastError(WGET_OPEN_REQUEST);
return NULL;
}
if(flags & WGET_POST){
extra_headers = LocalAlloc(LPTR, 2048);
if(extra_headers == NULL){
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
InternetCloseHandle(handles[2]);
InternetCloseHandle(handles[1]);
InternetCloseHandle(handles[0]);
SetLastError(WGET_INTERNAL_ALLOC);
return NULL;
}
sprintf(extra_headers, "Content-Type: %s\n"
"Content-Length: %d\n", content_type, content_len);
HttpSendRequest(handles[2], extra_headers, strlen(extra_headers), postdata, postdatalen);
}else{
HttpSendRequest(handles[2], NULL, 0, NULL, 0);
}
internal_len = 1024*1000*internal_mod;
internal_buf = LocalAlloc(LPTR, internal_len);
internal_idx = 0;
if(!internal_buf){
SafeLocalFree(extra_headers);
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
InternetCloseHandle(handles[2]);
InternetCloseHandle(handles[1]);
InternetCloseHandle(handles[0]);
SetLastError(WGET_INTERNAL_ALLOC);
return NULL;
}
while(InternetReadFile(handles[2], internal_buf + internal_idx, 1024, &temporals[1]) && temporals[1]){
if(internal_idx >= internal_len - 1024){
SafeLocalFree(internal_buf);
SafeLocalFree(extra_headers);
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
InternetCloseHandle(handles[2]);
InternetCloseHandle(handles[1]);
InternetCloseHandle(handles[0]);
SetLastError(WGET_INTERNAL_READFILE);
return NULL;
}
internal_idx += temporals[1];
}
retval = LocalAlloc(LPTR, internal_idx + 1024);
if(!retval){
SafeLocalFree(internal_buf);
SafeLocalFree(extra_headers);
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
InternetCloseHandle(handles[2]);
InternetCloseHandle(handles[1]);
InternetCloseHandle(handles[0]);
SetLastError(WGET_INTERNAL_RETURN_ALLOC);
return NULL;
}
memcpy(retval, internal_buf, internal_idx);
SafeLocalFree(internal_buf);
SafeLocalFree(extra_headers);
SafeLocalFree(urlComponents.lpszUrlPath);
SafeLocalFree(urlComponents.lpszHostName);
return retval;
}
#ifndef SW_HTTP_H
#include <stdio.h>
#include <winsock.h>
#include <wininet.h>
#include <sys/types.h>
#define SW_HTTP_H
//WGET FLAGS
#define WGET_HTTPS (((DWORD) 1) << 0)
#define WGET_POST (((DWORD) 1) << 1)
#define WGET_LITTE_FILE (((DWORD) 1) << 2)
#define WGET_MEDIUM_FILE (((DWORD) 1) << 3)
#define WGET_BIG_FILE (((DWORD) 1) << 4)
#define WGET_HUGE_FILE (((DWORD) 1) << 5)
#define WGET_PROXY_PASS (((DWORD) 1) << 6)
#define WGET_HTTP_PASS (((DWORD) 1) << 7)
//WGET FLAGS
//WGET ERROR CODES
#define WGET_CRACK_URL (((DWORD) 1) << 0)
#define WGET_INTERNET_OPEN (((DWORD) 1) << 1)
#define WGET_INTERNET_CONNECT (((DWORD) 1) << 2)
#define WGET_OPEN_REQUEST (((DWORD) 1) << 3)
#define WGET_INTERNAL_ALLOC (((DWORD) 1) << 4)
#define WGET_INTERNAL_READFILE (((DWORD) 1) << 5)
#define WGET_INTERNAL_RETURN_ALLOC (((DWORD) 1) << 6)
//WGET ERROR CODES
char *wget(char *url, DWORD flags, char *content_type, int content_len, char *postdata, int postdatalen, char *username, char *password);
char *wget_with_retryes(char *url, DWORD flags, char *content_type, int content_len, char *postdata, int postdatalen, char *username, char *password, int chances);
#endif