Skip to content

Instantly share code, notes, and snippets.

@gin1314
Created August 23, 2012 08:54
Show Gist options
  • Select an option

  • Save gin1314/3434391 to your computer and use it in GitHub Desktop.

Select an option

Save gin1314/3434391 to your computer and use it in GitHub Desktop.

Revisions

  1. gin1314 revised this gist Aug 23, 2012. 1 changed file with 33 additions and 24 deletions.
    57 changes: 33 additions & 24 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,8 @@ HANDLE ih;
    URL_COMPONENTS url;
    HINTERNET ch, req;
    DWORD dwRead;
    char szBuffer[1024];
    // char szBuffer[1024];
    BYTE szBuffer = 0;
    LPVOID lpMsgBuf;
    const char *accept = "*/*";
    char *error = "error visiting URL.";
    @@ -86,29 +87,37 @@ HANDLE ih;
    if (HttpSendRequest(req, NULL, 0, NULL, 0)) {
    if (!visit.silent)
    printf("%s\n", "success HttpSendRequest");
    if ( InternetReadFile(req, &szBuffer, 1024, &dwRead) ) {
    printf("%s\n", &szBuffer);
    } else {

    FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    0, // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL
    );
    // Process any inserts in lpMsgBuf.
    // ...
    // Display the string.
    printf("%s\n", lpMsgBuf);
    // Free the buffer.
    LocalFree( lpMsgBuf );

    }
    // if ( InternetReadFile(req, &szBuffer, 1024, &dwRead) ) {
    // printf("%s\n", &szBuffer);
    // } else {

    // FormatMessage(
    // FORMAT_MESSAGE_ALLOCATE_BUFFER |
    // FORMAT_MESSAGE_FROM_SYSTEM |
    // FORMAT_MESSAGE_IGNORE_INSERTS,
    // NULL,
    // GetLastError(),
    // 0, // Default language
    // (LPTSTR) &lpMsgBuf,
    // 0,
    // NULL
    // );
    // // Process any inserts in lpMsgBuf.
    // // ...
    // // Display the string.
    // printf("%s\n", lpMsgBuf);
    // // Free the buffer.
    // LocalFree( lpMsgBuf );

    // }

    while( InternetReadFile(req, &szBuffer, 1, &dwRead ) == TRUE ) {
    if (dwRead == 0)
    {
    break;
    }
    printf("%s", &szBuffer );
    }

    // irc_privmsg(visit.sock, visit.chan, "url visited.", FALSE);
    } else {
  2. gin1314 revised this gist Aug 23, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ HANDLE ih;
    URL_COMPONENTS url;
    HINTERNET ch, req;
    DWORD dwRead;
    char *szBuffer;
    char szBuffer[1024];
    LPVOID lpMsgBuf;
    const char *accept = "*/*";
    char *error = "error visiting URL.";
    @@ -86,10 +86,10 @@ HANDLE ih;
    if (HttpSendRequest(req, NULL, 0, NULL, 0)) {
    if (!visit.silent)
    printf("%s\n", "success HttpSendRequest");
    if ( InternetReadFile(req, &szBuffer, 100, &dwRead) ) {
    if ( InternetReadFile(req, &szBuffer, 1024, &dwRead) ) {
    printf("%s\n", &szBuffer);
    } else {
    ;

    FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
  3. gin1314 revised this gist Aug 23, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ HANDLE ih;
    URL_COMPONENTS url;
    HINTERNET ch, req;
    DWORD dwRead;
    LPVOID szBuffer;
    char *szBuffer;
    LPVOID lpMsgBuf;
    const char *accept = "*/*";
    char *error = "error visiting URL.";
    @@ -86,8 +86,8 @@ HANDLE ih;
    if (HttpSendRequest(req, NULL, 0, NULL, 0)) {
    if (!visit.silent)
    printf("%s\n", "success HttpSendRequest");
    if ( InternetReadFile(req, TEXT(szBuffer), MAX_PATH, &dwRead) ) {

    if ( InternetReadFile(req, &szBuffer, 100, &dwRead) ) {
    printf("%s\n", &szBuffer);
    } else {
    ;
    FormatMessage(
  4. gin1314 revised this gist Aug 23, 2012. 1 changed file with 27 additions and 4 deletions.
    31 changes: 27 additions & 4 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -17,12 +17,13 @@ typedef struct vs {

    HANDLE ih;

    LPVOID visit(LPVOID param)
    VOID visit(LPVOID param)
    {
    URL_COMPONENTS url;
    HINTERNET ch, req;
    DWORD dwRead;
    char *szBuffer;
    LPVOID szBuffer;
    LPVOID lpMsgBuf;
    const char *accept = "*/*";
    char *error = "error visiting URL.";
    vs visit;
    @@ -85,8 +86,30 @@ HANDLE ih;
    if (HttpSendRequest(req, NULL, 0, NULL, 0)) {
    if (!visit.silent)
    printf("%s\n", "success HttpSendRequest");
    InternetReadFile(req, TEXT(szBuffer), MAX_PATH, &dwRead);
    printf("%s\n", (char *)szBuffer);
    if ( InternetReadFile(req, TEXT(szBuffer), MAX_PATH, &dwRead) ) {

    } else {
    ;
    FormatMessage(
    FORMAT_MESSAGE_ALLOCATE_BUFFER |
    FORMAT_MESSAGE_FROM_SYSTEM |
    FORMAT_MESSAGE_IGNORE_INSERTS,
    NULL,
    GetLastError(),
    0, // Default language
    (LPTSTR) &lpMsgBuf,
    0,
    NULL
    );
    // Process any inserts in lpMsgBuf.
    // ...
    // Display the string.
    printf("%s\n", lpMsgBuf);
    // Free the buffer.
    LocalFree( lpMsgBuf );

    }

    // irc_privmsg(visit.sock, visit.chan, "url visited.", FALSE);
    } else {
    if (!visit.silent)
  5. gin1314 created this gist Aug 23, 2012.
    120 changes: 120 additions & 0 deletions test.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <winsock.h>
    #include <wininet.h>
    #include <shellapi.h>
    #include <mmsystem.h>

    typedef struct vs {
    char host[128];
    char referer[128];
    char chan[128];
    SOCKET sock;
    BOOL silent;
    BOOL gotinfo;
    } vs;

    HANDLE ih;

    LPVOID visit(LPVOID param)
    {
    URL_COMPONENTS url;
    HINTERNET ch, req;
    DWORD dwRead;
    char *szBuffer;
    const char *accept = "*/*";
    char *error = "error visiting URL.";
    vs visit;
    char vhost[128];
    int vport;
    char vuser[128];
    char vpass[128];
    char vpath[256];

    visit = *((vs *)param);
    vs *vsp = (vs *)param;
    vsp->gotinfo = TRUE;

    // zero out string varaiables
    memset(vhost, 0, sizeof(vhost));
    memset(vuser, 0, sizeof(vuser));
    memset(vpass, 0, sizeof(vpass));
    memset(vpath, 0, sizeof(vpath));

    // zero out url structure and set options
    memset(&url, 0, sizeof(url));
    url.dwStructSize = sizeof(url);
    url.dwHostNameLength = 1;
    url.dwUserNameLength = 1;
    url.dwPasswordLength = 1;
    url.dwUrlPathLength = 1;

    do {
    // crack the url (break it into its main parts)
    if (!InternetCrackUrl(visit.host, strlen(visit.host), 0, &url)) {
    if (!visit.silent)
    printf("%s\n", "invalid url");
    // irc_privmsg(visit.sock, visit.chan, "invalid URL.", FALSE);
    break;
    }

    // copy url parts into variables
    if (url.dwHostNameLength > 0) strncpy(vhost, url.lpszHostName, url.dwHostNameLength);
    vport = url.nPort;
    if (url.dwUserNameLength > 0) strncpy(vuser, url.lpszUserName, url.dwUserNameLength);
    if (url.dwPasswordLength > 0) strncpy(vpass, url.lpszPassword, url.dwPasswordLength);
    if (url.dwUrlPathLength > 0) strncpy(vpath, url.lpszUrlPath, url.dwUrlPathLength);

    ch = InternetConnect(ih, vhost, vport, vuser, vpass, INTERNET_SERVICE_HTTP, 0, 0);
    if (ch == NULL) {
    if (!visit.silent)
    printf("%s\n", "error InternetConnect");
    // irc_privmsg(visit.sock, visit.chan, error, FALSE);
    break;
    }

    req = HttpOpenRequest(ch, NULL, vpath, NULL, visit.referer, &accept, INTERNET_FLAG_NO_UI, 0);
    if (req == NULL) {
    if (!visit.silent)
    printf("%s\n", "error HttpOpenRequest");
    // irc_privmsg(visit.sock, visit.chan, error, FALSE);
    break;
    }

    if (HttpSendRequest(req, NULL, 0, NULL, 0)) {
    if (!visit.silent)
    printf("%s\n", "success HttpSendRequest");
    InternetReadFile(req, TEXT(szBuffer), MAX_PATH, &dwRead);
    printf("%s\n", (char *)szBuffer);
    // irc_privmsg(visit.sock, visit.chan, "url visited.", FALSE);
    } else {
    if (!visit.silent)
    printf("%s\n", "error HttpSendRequest");
    // irc_privmsg(visit.sock, visit.chan, error, FALSE);
    }
    } while(0); // always false, so this never loops, only helps make error handling easier

    InternetCloseHandle(ch);
    InternetCloseHandle(req);
    // return 0;
    }

    int main(int argc, char const *argv[])
    {
    vs vis;

    ih = InternetOpen("Mozilla/4.0 (compatible)", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    if (ih == NULL)
    {
    printf("%s\n", "null");
    }

    strncpy(vis.host, "http://localhost/paypal/cprog.php", sizeof(vis.host)-1);
    vis.silent = FALSE;

    visit(&vis);

    return 0;
    }
    // compile with : g++ -Wall -O test.c -o test.exe -L/c/mingw/lib -lwsock32 -lwininet