/*** ==================================================== * httppost.c - ( see httppost.h ) * ====================================================== **/ #include "include/httppost.h" #include "include/sau_socket.h" struct HPData * makeHttpPost ( char * Host, char * Path, char * Form, char * Browser, char * DataType, unsigned long DataSize ) { char cError [ 1024 ] ; unsigned long ulFormSize = sizeof ( Form ) ; unsigned long ulDataSize = DataSize + 20 ; unsigned long ulDataTypeSize = sizeof ( DataType ) + 20 ; struct HPData * HTTPPost = ( struct HPData * ) malloc ( sizeof( struct HPData ) ); if ( HTTPPost == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( sizeof( struct HPData ) ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } memset ( HTTPPost, 0x00, sizeof ( struct HPData ) ) ; snprintf ( HTTPPost->cHost, HTDHOST - 1, "Host: %s", Host ) ; snprintf ( HTTPPost->cPath, HTDPATH - 1, "%s", Path ) ; HTTPPost->pcForm = ( char * ) malloc ( ulFormSize ) ; if ( HTTPPost->pcForm == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( ulFormSize ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } memcpy ( HTTPPost->pcForm, Form, ulFormSize ) ; snprintf ( HTTPPost->cConType, HTDCON - 1, "Connection:close" ) ; snprintf ( HTTPPost->cBrowser, HTDBROWSER - 1, "User-Agent: %s", Browser ) ; HTTPPost->pcDataSize = ( char * ) malloc ( ulDataSize ) ; if ( HTTPPost->pcDataSize == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( ulDataSize ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( HTTPPost->pcDataSize, ulDataSize - 1, "Content-Length: %ld", ulDataSize - 20 ) ; HTTPPost->pcDataType = ( char * ) malloc ( ulDataTypeSize ) ; if ( HTTPPost->pcDataType == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( ulDataTypeSize ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( HTTPPost->pcDataType, ulDataTypeSize - 1, "Content-Type: %s", DataType ) ; return HTTPPost ; } int sendHttpPost ( struct HPData * httppost, PSTR host, WPORT port ) { unsigned long ulLength = 0 ; unsigned long ulFDLength = 0 ; WSOCK sock; char cError [ 1024 ] ; char * pcPostData = ( char * ) malloc ( HTDPOST + HTDPATH + HTDHTTPVER ) ; if ( pcPostData == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( HTDPOST + HTDPATH + HTDHTTPVER ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( pcPostData, ( HTDPOST + HTDPATH + HTDHTTPVER ) - 1, "POST %s HTTP/1.0\r\n\r\n", httppost->cPath ) ; ulLength = strlen ( pcPostData ) ; pcPostData = ( char * ) realloc ( pcPostData, HTDHOST + ulLength ) ; if ( pcPostData == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( HTDHOST + ulLength ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( pcPostData, HTDHOST + ulLength - 1, "%s\r\n\r\n", httppost->cHost ) ; ulLength = strlen ( pcPostData ) ; ulFDLength = httppost->pcDataType ; pcPostData = ( char * ) realloc ( pcPostData, ulFDLength + ulLength ) ; if ( pcPostData == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( ulFDLength + ulLength ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( pcPostData, ulFDLength + ulLength - 1, "%s\r\n\r\n", httppost->pcDataType ) ; ulLength = strlen ( pcPostData ) ; ulFDLength = httppost->pcDataSize ; pcPostData = ( char * ) realloc ( pcPostData, ulFDLength + ulLength ) ; if ( pcPostData == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( ulFDLength + ulLength ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( pcPostData, ulFDLength + ulLength - 1, "%s\r\n\r\n", httppost->pcDataType ) ; ulLength = strlen ( pcPostData ) ; pcPostData = ( char * ) realloc ( pcPostData, HTDBROWSER + ulLength ) ; if ( pcPostData == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( HTDBROWSER + ulLength ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( pcPostData, HTDBROWSER + ulLength - 1, "%s\r\n\r\n", httppost->cBrowser ) ; ulLength = strlen ( pcPostData ) ; pcPostData = ( char * ) realloc ( pcPostData, HDTCON + ulLength ) ; if ( pcPostData == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( HDTCON + ulLength ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( pcPostData, HDTCON + ulLength - 1, "%s\r\n\r\n", httppost->cConType ) ; ulLength = strlen ( pcPostData ) ; ulFDLength = strlen ( httppost->pcFormData ) ; pcPostData = ( char * ) realloc ( pcPostData, HTDFORM + ulLength + ulFDLength ) ; if ( pcPostData == NULL ) { snprintf ( cError, 1023, "Couldn't allocate Memory: %d Bytes", ( HDTFORM + ulFDLength + ulLength ) ) ; MessageBox ( NULL, cError, "Not enough Memory", MB_ICONERROR ) ; return EXIT_FAILURE ; } snprintf ( pcPostData, HDTFORM + ulLength ulFDLength - 1, "%s%s\r\n\r\n", httppost->cForm, httppost->pcFormData ) ; if ( sCreate ( sock ) == EXIT_FAILURE ) { return EXIT_FAILURE ; } if ( sConnect ( sock, host, port ) == EXIT_FAILURE ) { return EXIT_FAILURE ; } if ( sSend ( sock, pcPostData ) == EXIT_FAILURE ) { return EXIT_FAILURE ; } sClose ( sock ) ; free ( pcPostData ) ; return EXIT_SUCCESS ; } int freeHttpData ( struct HPData * httppost ) { if ( httppost != NULL ) { if ( httppost->pcFormData != NULL ) { free ( httppost->pcFormData ) ; } if ( httppost->pcDataSize != NULL ) { free ( httppost->pcDataSize ) ; } if ( httppost->pcDataType != NULL ) { free ( httppost->pcDataType ) ; } free ( httppost ) ; } else { MessageBox ( NULL, "the HPData struct never got allocated", "Information", MB_ICONEXCLAMATION ) ; } return EXIT_SUCCESS ; }