📄 gethttp.c
字号:
#include "gethttp.h"void memtofile( FILE *fp,const char *pdate, unsigned int len ){ char buf[1024]; char *p; int i; if ( fp == NULL || pdate == NULL ) return ; i = len; p = (char *)pdate; while( i > 0 ) { memset( buf, 0, sizeof(buf) ); if ( i < sizeof(buf) )
{ memcpy( buf, p, i );
i = 0; } else { memcpy( buf, p, sizeof(buf) - 1 ); i -= ( sizeof(buf) - 1 ); p += ( sizeof(buf) - 1 ); } fprintf( fp, "%s", buf ); }}int GetHttp( char * url, char * filename ){ char *Server; char *URL; char *Verb = "GET"; //GET POST char *Gateway = NULL; char *AcceptTypes[2] = {"*/*", NULL}; char Headers[] = "Accept: */*\r\n" "User-Agent: Httpget\r\n" "Referer: Httpget\r\n" "\r\n"; int Method = SOCKETS_METHOD; int DisplayHeaders = FALSE; int ClientDataSize = 0; char pszUserName[32]; char pszPassword[32]; char pszStore[64]; char *pszPref = NULL; char server[MAX_STR_LEN+ 1]; char object[LKHD_URL_BUF_LEN+1 ]; unsigned short nPort; memset ( pszUserName, 0, sizeof(pszUserName) ); memset ( pszPassword, 0, sizeof(pszPassword) ); memset ( pszStore, 0, sizeof(pszStore) ); memset ( server, 0, sizeof(server) ); memset ( object, 0, sizeof(object) ); strcpy( pszStore, filename ); if ( ParsUrl ( url, server, object, &nPort ) == 0 ) { printf(" Error parse \n"); return 0; } else { printf("\tserver :%s\n\tobject :%s\n\tport :%d\n",server, object, nPort); } Server = server; // URL = url; URL = object; if ( HttpGetSocket( Verb, Server, URL, DisplayHeaders, ClientDataSize, pszUserName, pszPassword, pszStore, pszPref, nPort) == FALSE ) { printf(" http get return Error!\n"); return 0;// waitfor multiple object 1: thread 2: hangup signal. } else { return 1; } }int ParsUrl ( char * url, char * server, char * object, unsigned short * nPort ){ char* pdest; char* tempBuffer; char* pPort; int len; if (( url == NULL) || (server == NULL) || (object == NULL) ) { printf("url == NULL | server == NULL | object == NULL\n"); return 0; }#ifdef UNIX if ( strncasecmp( url, "http://", 7 ) != 0 )#else if ( _strnicmp( url, "http://", 7 ) != 0 )#endif { printf("Error: can only use URLs beginning with http://\n"); return 0; } pdest = strchr ( &(url[7]), '/'); if ( pdest == NULL ) { printf(" url no Server \n"); return 0; } tempBuffer = (char *) malloc ( pdest - url -7 +1 ); if ( tempBuffer == NULL ) { printf(" tempBuffer is NULL \n"); return 0; } memset ( tempBuffer, (char)0, pdest - url -7 +1); memcpy ( tempBuffer, &(url[7]), pdest - url -7 ); pPort = strchr( tempBuffer, ':'); if ( pPort == NULL ) { *nPort = 80; strncpy ( server, tempBuffer, (pdest - url -7) < MAX_STR_LEN ? (pdest - url -7):MAX_STR_LEN ); } else { *nPort = atoi ( &(pPort[1])); strncpy ( server, tempBuffer , ( pPort - tempBuffer ) < MAX_STR_LEN ? (pPort - tempBuffer ):MAX_STR_LEN ); } strncpy ( object, pdest, LKHD_URL_BUF_LEN ); for( len = 0; (unsigned)len < strlen ( object ); len ++ ) { if ( object[ len ] == ' ' || object[ len ] == ' ' ) object[ len ] = '+'; } free (tempBuffer ); return 1;}int HttpGetSocket( char * Verb, char * Server, char * URL, int DisplayHeaders, int ClientDataSize, char *pchUserName, char *pchPassword, char *pszStore, char *pszPref, unsigned short nPort ) /*++ Routine Description: Issue a command to a HTTP server using authentication Arguments: Verb HTTP command : GET / HEAD / ... Server server name URL URL to send to the server DisplayHeaders TRUE to display headers as received ClientDataSize number of bytes to send in the request pchUserName user name for authentication pchPassword password for authentication pszStore file name where to dump reply from server Return Value: Returns TRUE is successful; otherwise FALSE is returned. --*/{ // char achAuth[128]; char ReceiveBuffer[8*1024]; int Error; char Request[1024]; int RequestSize; char * AcceptTypes[2] = {"*/*", NULL}; int Socket; struct sockaddr_in Address; struct hostent * HostEntry; char Headers1[] = "HTTP/1.1\r\n"; char Headers2[] = "Accept: text/*\r\n" "User-Agent: IVRClient\r\n" "Pragma: no-cache\r\n" "Cache-control: no-catch\r\n" "Connection: close\r\n" // "If-Match: 802580bfb083c11:285d\r\n" // "Range: bytes=0-\r\n" ; // ""; //; char tailBuffer[3*1024]; char portBuffer[64]; char CrLf[] = "\r\n"; char ClientData[64*1024]; int fKeepAlive = FALSE; int cRec; int cLen; int fInHeader; // PSTR pchAuthData; int fServerKeepAlive = FALSE; // int fNeedMoreData; // int fNeedAuthenticate; char *pH; char *pN; int fStatusLine; int Status = -1; int cToRead; // PSTR paAuth = achAuth; int fSt = FALSE; int hnd = -1; FILE *fp; // char* bufferPtr; int fGetChunkLen = TRUE; int etagflag = 0; char etagbuffer[256]; int modifyflag = 0; char modifybuffer[256]; //fd_set fd; //TIMEVAL tv = { 15, 0 }; int chunkedLen; int sockettimeout; time_t tt; memset( portBuffer, 0, sizeof(portBuffer) ); memset( ReceiveBuffer, 0, sizeof(ReceiveBuffer) ); sprintf( portBuffer, ":%d\r\n", nPort); // open write file fp = NULL; if ( pszStore != NULL && pszStore[0] != '\0' ) { if ( ( fp = fopen( pszStore, "a" ) ) == NULL ) { printf( "Can't create file %s\n", pszStore ); } tt = time(NULL); fprintf( fp, "%s", ctime(&tt) ); memtofile( fp, URL, strlen(URL) ); memtofile( fp, CrLf, strlen(CrLf) ); } // open socket, connect to server // if ( Socket == INVALID_SOCKET ) { int lenttt = sizeof( int ); Socket = socket(AF_INET, SOCK_STREAM, 0); if (Socket == INVALID_SOCKET) { fprintf(stderr, "Error creating socket = %s\n", strerror(errno)); fSt = FALSE; goto ex; } Error = getsockopt ( Socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&sockettimeout, &lenttt); if ( Error ) { fprintf(stderr, "Error getsockopt %s\n", strerror(errno)); } else { printf(" get timeout %d\n", sockettimeout); } sockettimeout = 15000; Error = setsockopt ( Socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&sockettimeout, sizeof( int )); if ( Error ) { fprintf(stderr, "Error getsockopt %s\n", strerror(errno)); } else { printf(" timeout %d\n", sockettimeout); } Error = getsockopt ( Socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&sockettimeout, &lenttt); if ( Error ) { fprintf(stderr, "Error getsockopt %s\n", strerror(errno)); } else { printf(" get timeout %d\n", sockettimeout); } Address.sin_family = AF_INET; Address.sin_port = 0; Address.sin_addr.s_addr = INADDR_ANY; Error = bind( Socket, (struct sockaddr *) &Address, sizeof(Address)); if (Error) { fprintf(stderr, "Error in bind = %s\n", strerror(errno)); fSt = FALSE; goto ex; } Address.sin_family = AF_INET; Address.sin_port = htons(nPort); Address.sin_addr.s_addr = inet_addr(Server); if (Address.sin_addr.s_addr == -1) { // // Must be a server name // HostEntry = gethostbyname(Server); if (HostEntry == NULL) { printf("unable to resolve %s\n", Server); fSt = FALSE; goto ex; } else { Address.sin_addr.s_addr = *((unsigned long *) HostEntry->h_addr); } } /* A successful WSAStartup must occur before using this function. The network subsystem has failed. One of the optval or the optlen parameters is not a valid part of the user address space, or the optlen parameter is too small. A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. The level parameter is unknown or invalid. The option is unknown or unsupported by the indicated protocol family. */ Error = connect( Socket, (struct sockaddr *) &Address, sizeof(Address)); if (Error) { fprintf(stderr, "Error connecting to %s = %s\n", Server, strerror(errno)); fSt = FALSE; goto ex; } } // // Send the client request // memset( Request, (char)0, 1023); // method
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -