Exemplo n.º 1
0
// not working in mingw. test.xml file is not saved properly as text file.
void FetchXMLFile() {
#if defined(LIBXML_HTTP_ENABLED)
    int ret_val;
    char *contentType; /* if available the Content-Type information will be returned at that location */

	xmlNanoHTTPInit();
	
	ret_val = xmlNanoHTTPFetch(XML_URL, "test.xml", &contentType);
	
	printf("ContentType: %s\n",contentType);
	printf("Retval %d\n", ret_val);
	
	xmlNanoHTTPCleanup();
#endif
}
Exemplo n.º 2
0
void GetXMLHttp() {
	void * Ctxt;
	char *contentType;
	
	xmlNanoHTTPInit();
	
	Ctxt = xmlNanoHTTPOpen(XML_URL, &contentType);

	if (Ctxt == 0)
		printf("ERROR: xmlNanoHTTPOpen() == 0\n");

	if (xmlNanoHTTPReturnCode(Ctxt) != 200)
		printf("ERROR: HTTP Code != OK\n");

	// open output file
	FILE *fp;
	fp=fopen("test_get.xml","w");
	

	// Write to file
	const int Size = 2048;
	char Buffer[Size];
	int Count;

	while ((Count = xmlNanoHTTPRead(Ctxt, Buffer, Size)) > 0)
		fprintf(fp,"%s",Buffer);	
		//File.write(Buffer, Count);
		
	fclose(fp);

	if (Count == 0)
		printf("STATUS: Connection closed\n");

	if (Count == -1)
		printf("ERROR: xmlNanoHTTPRead() == -1\n");
	
		
	// Free ressources
	xmlFree(contentType);
	xmlNanoHTTPClose(Ctxt);
	xmlNanoHTTPCleanup();
}
Exemplo n.º 3
0
void*
xmlNanoHTTPMethodRedir(const char *URL, const char *method, const char *input,
                  char **contentType, char **redir,
		  const char *headers, int ilen ) {
    xmlNanoHTTPCtxtPtr ctxt;
    char *bp, *p;
    int blen;
    SOCKET ret;
    int nbRedirects = 0;
    char *redirURL = NULL;
#ifdef DEBUG_HTTP
    int xmt_bytes;
#endif
    
    if (URL == NULL) return(NULL);
    if (method == NULL) method = "GET";
    xmlNanoHTTPInit();

retry:
    if (redirURL == NULL)
	ctxt = xmlNanoHTTPNewCtxt(URL);
    else {
	ctxt = xmlNanoHTTPNewCtxt(redirURL);
	ctxt->location = xmlMemStrdup(redirURL);
    }

    if ( ctxt == NULL ) {
	return ( NULL );
    }

    if ((ctxt->protocol == NULL) || (strcmp(ctxt->protocol, "http"))) {
	__xmlIOErr(XML_FROM_HTTP, XML_HTTP_URL_SYNTAX, "Not a valid HTTP URI");
        xmlNanoHTTPFreeCtxt(ctxt);
	if (redirURL != NULL) xmlFree(redirURL);
        return(NULL);
    }
    if (ctxt->hostname == NULL) {
	__xmlIOErr(XML_FROM_HTTP, XML_HTTP_UNKNOWN_HOST,
	           "Failed to identify host in URI");
        xmlNanoHTTPFreeCtxt(ctxt);
	if (redirURL != NULL) xmlFree(redirURL);
        return(NULL);
    }
    if (proxy) {
	blen = strlen(ctxt->hostname) * 2 + 16;
	ret = xmlNanoHTTPConnectHost(proxy, proxyPort);
    }
    else {
	blen = strlen(ctxt->hostname);
	ret = xmlNanoHTTPConnectHost(ctxt->hostname, ctxt->port);
    }
    if (ret == INVALID_SOCKET) {
        xmlNanoHTTPFreeCtxt(ctxt);
	if (redirURL != NULL) xmlFree(redirURL);
        return(NULL);
    }
    ctxt->fd = ret;

    if (input == NULL)
	ilen = 0;
    else
	blen += 36;

    if (headers != NULL)
	blen += strlen(headers) + 2;
    if (contentType && *contentType)
	/* reserve for string plus 'Content-Type: \r\n" */
	blen += strlen(*contentType) + 16;
    if (ctxt->query != NULL)
	/* 1 for '?' */
	blen += strlen(ctxt->query) + 1;
    blen += strlen(method) + strlen(ctxt->path) + 24;
#ifdef HAVE_ZLIB_H
    /* reserve for possible 'Accept-Encoding: gzip' string */
    blen += 23;
#endif
    if (ctxt->port != 80) {
	/* reserve space for ':xxxxx', incl. potential proxy */
	if (proxy)
	    blen += 12;
	else
	    blen += 6;
    }
    bp = (char*)xmlMallocAtomic(blen);
    if ( bp == NULL ) {
        xmlNanoHTTPFreeCtxt( ctxt );
	xmlHTTPErrMemory("allocating header buffer");
	return ( NULL );
    }

    p = bp;

    if (proxy) {
	if (ctxt->port != 80) {
	    p += snprintf( p, blen - (p - bp), "%s http://%s:%d%s", 
			method, ctxt->hostname,
		 	ctxt->port, ctxt->path );
	}
	else 
	    p += snprintf( p, blen - (p - bp), "%s http://%s%s", method,
	    		ctxt->hostname, ctxt->path);
    }
    else
	p += snprintf( p, blen - (p - bp), "%s %s", method, ctxt->path);

    if (ctxt->query != NULL)
	p += snprintf( p, blen - (p - bp), "?%s", ctxt->query);

    if (ctxt->port == 80) {
        p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s\r\n", 
		    ctxt->hostname);
    } else {
        p += snprintf( p, blen - (p - bp), " HTTP/1.0\r\nHost: %s:%d\r\n",
		    ctxt->hostname, ctxt->port);
    }

#ifdef HAVE_ZLIB_H
    p += snprintf(p, blen - (p - bp), "Accept-Encoding: gzip\r\n");
#endif

    if (contentType != NULL && *contentType) 
	p += snprintf(p, blen - (p - bp), "Content-Type: %s\r\n", *contentType);

    if (headers != NULL)
	p += snprintf( p, blen - (p - bp), "%s", headers );

    if (input != NULL)
	snprintf(p, blen - (p - bp), "Content-Length: %d\r\n\r\n", ilen );
    else
	snprintf(p, blen - (p - bp), "\r\n");

#ifdef DEBUG_HTTP
    xmlGenericError(xmlGenericErrorContext,
	    "-> %s%s", proxy? "(Proxy) " : "", bp);
    if ((blen -= strlen(bp)+1) < 0)
	xmlGenericError(xmlGenericErrorContext,
		"ERROR: overflowed buffer by %d bytes\n", -blen);
#endif
    ctxt->outptr = ctxt->out = bp;
    ctxt->state = XML_NANO_HTTP_WRITE;
    blen = strlen( ctxt->out );
#ifdef DEBUG_HTTP
    xmt_bytes = xmlNanoHTTPSend(ctxt, ctxt->out, blen );
    if ( xmt_bytes != blen )
        xmlGenericError( xmlGenericErrorContext,
			"xmlNanoHTTPMethodRedir:  Only %d of %d %s %s\n",
			xmt_bytes, blen,
			"bytes of HTTP headers sent to host",
			ctxt->hostname );
#else
    xmlNanoHTTPSend(ctxt, ctxt->out, blen );
#endif

    if ( input != NULL ) {
#ifdef DEBUG_HTTP
        xmt_bytes = xmlNanoHTTPSend( ctxt, input, ilen );

	if ( xmt_bytes != ilen )
	    xmlGenericError( xmlGenericErrorContext,
	    		"xmlNanoHTTPMethodRedir:  Only %d of %d %s %s\n",
			xmt_bytes, ilen,
			"bytes of HTTP content sent to host",
			ctxt->hostname );
#else
	xmlNanoHTTPSend( ctxt, input, ilen );
#endif
    }

    ctxt->state = XML_NANO_HTTP_READ;

    while ((p = xmlNanoHTTPReadLine(ctxt)) != NULL) {
        if (*p == 0) {
	    ctxt->content = ctxt->inrptr;
	    xmlFree(p);
	    break;
	}
	xmlNanoHTTPScanAnswer(ctxt, p);

#ifdef DEBUG_HTTP
	xmlGenericError(xmlGenericErrorContext, "<- %s\n", p);
#endif
        xmlFree(p);
    }

    if ((ctxt->location != NULL) && (ctxt->returnValue >= 300) &&
        (ctxt->returnValue < 400)) {
#ifdef DEBUG_HTTP
	xmlGenericError(xmlGenericErrorContext,
		"\nRedirect to: %s\n", ctxt->location);
#endif
	while ( xmlNanoHTTPRecv(ctxt) > 0 ) ;
        if (nbRedirects < XML_NANO_HTTP_MAX_REDIR) {
	    nbRedirects++;
	    if (redirURL != NULL)
		xmlFree(redirURL);
	    redirURL = xmlMemStrdup(ctxt->location);
	    xmlNanoHTTPFreeCtxt(ctxt);
	    goto retry;
	}
	xmlNanoHTTPFreeCtxt(ctxt);
	if (redirURL != NULL) xmlFree(redirURL);
#ifdef DEBUG_HTTP
	xmlGenericError(xmlGenericErrorContext,
		"xmlNanoHTTPMethodRedir: Too many redirects, aborting ...\n");
#endif
	return(NULL);
    }

    if (contentType != NULL) {
	if (ctxt->contentType != NULL)
	    *contentType = xmlMemStrdup(ctxt->contentType);
	else
	    *contentType = NULL;
    }

    if ((redir != NULL) && (redirURL != NULL)) {
	*redir = redirURL;
    } else {
	if (redirURL != NULL)
	    xmlFree(redirURL);
	if (redir != NULL)
	    *redir = NULL;
    }

#ifdef DEBUG_HTTP
    if (ctxt->contentType != NULL)
	xmlGenericError(xmlGenericErrorContext,
		"\nCode %d, content-type '%s'\n\n",
	       ctxt->returnValue, ctxt->contentType);
    else
	xmlGenericError(xmlGenericErrorContext,
		"\nCode %d, no content-type\n\n",
	       ctxt->returnValue);
#endif

    return((void *) ctxt);
}
Exemplo n.º 4
0
void CheckVersionNumber()
{
	//==== Init Nano HTTP ====//
	xmlNanoHTTPInit();
	xmlNanoHTTPScanProxy(NULL);

	//==== Compute Version Number ====//
	int ver_no = 10000*VSP_VERSION_MAJOR + 100*VSP_VERSION_MINOR + VSP_VERSION_CHANGE;

	char cCurrentPath[FILENAME_MAX];
	GetCurrentDir(cCurrentPath, sizeof(cCurrentPath));

	int user_id = 0;
	int path_len = strlen(cCurrentPath);

	for ( int i = 0 ; i < path_len ; i++ )
	{
		srand ( (unsigned int)cCurrentPath[i] );
		user_id += rand() % 100000 + 1;
	}

	//==== Post User Info To Server ====//
	char poststr[256];
	sprintf( poststr, "postvar1=%d&postvar2=%d\r\n", user_id, ver_no);
	int poststrlen = strlen(poststr);
	char*  headers = "Content-Type: application/x-www-form-urlencoded \n";

	void * ctx = 0;
	ctx = xmlNanoHTTPMethod("http://www.openvsp.org/vspuse_post.php", "POST", poststr, NULL, headers, poststrlen );

	if ( ctx )
		xmlNanoHTTPClose(ctx);

	ctx = 0;

	//==== Open Settings File ====//
	bool check_version_flag = true;
	FILE* vsptime_fp = fopen( ".vsptime", "r" );
	if ( vsptime_fp )
	{
		char str[256];
		fgets( str, 256, vsptime_fp );
		int vsp_time = atoi( str );
		int del_time =  (int)time(NULL) - vsp_time;
		if ( del_time < 60*60*24*7 )				// Check Every Week
			check_version_flag = false;

		fclose( vsptime_fp );
	}

	//==== Enough Time Has Passed - Check For New Version ====//
	if ( check_version_flag )
	{
		//==== Webpage with Version Info ====//
		char * pContentType = 0;
		ctx = xmlNanoHTTPOpen("http://www.openvsp.org/latest_version.html", &pContentType);

		int retCode = xmlNanoHTTPReturnCode(ctx);

		//==== Http Return Code 200 -> OK  ====//
		string contentStr;
		if ( retCode == 200 )
		{
			char buf[2048];
			int len = 1;
			while (len > 0 && contentStr.size() < 10000 )
			{
				len = xmlNanoHTTPRead(ctx, buf, sizeof(buf));
				contentStr.append( buf, len );
			}
		}

		//==== Pulled A String From Server =====//
		if ( contentStr.size() > 0 )
		{
			int major_ver, minor_ver, change_ver;
			bool valid = ExtractVersionNumber( contentStr, &major_ver, &minor_ver, &change_ver );

			if ( valid )
			{
				if ( major_ver != VSP_VERSION_MAJOR ||minor_ver != VSP_VERSION_MINOR || change_ver != VSP_VERSION_CHANGE )
				{
					if ( screenMgrPtr )
						screenMgrPtr->MessageBox("A new version of OpenVSP is available at http://www.openvsp.org/");
				}
			}
		}
		//===== Write Time =====//
		FILE* vsptime_fp = fopen( ".vsptime", "w" );
		if ( vsptime_fp )
		{
			fprintf( vsptime_fp, "%d", time(NULL) );
			fclose( vsptime_fp );
		}
	}

	if ( ctx )
		xmlNanoHTTPClose(ctx);

	xmlNanoHTTPCleanup();
}
Exemplo n.º 5
0
/**
 * Returns the contents of the requested URL
 *
 * @param pczURL The URL to retrieve.
 * @param piRetCode The return code supplied with the response.
 * @param piDataSize The resulting data length [out].
 *
 * @return A pointer to a null-terminated buffer containing the textual
 *         representation of the response. Must be freed by the caller.
 */
gpointer
getURL(const gchar * pczURL, gint * piRetCode, gint * piDataSize)
{
    /* nanohttp magic */
#define iBufReadSize 1024
    gint iReadSize = 0;
    gint iCurrSize = 0;

    gpointer pInBuffer = NULL;
    gpointer pInBufferRef = NULL;

    gchar cReadBuffer[iBufReadSize];
    bzero(cReadBuffer, iBufReadSize);

    xmlNanoHTTPInit();

    char * pContentType = NULL;
    void * pHTTPContext = NULL;

    pHTTPContext = xmlNanoHTTPOpen(pczURL, &pContentType);

    if (!pHTTPContext)
    {
        // failure
        cleanup(pHTTPContext, pContentType);

        *piRetCode = -1;

        return pInBuffer; // it's NULL
    }

    *piRetCode = xmlNanoHTTPReturnCode(pHTTPContext);

    if (*piRetCode != HTTP_STATUS_OK)
    {
        // failure
        cleanup(pHTTPContext, pContentType);

        return pInBuffer; // it's NULL
    }

    while ((iReadSize = xmlNanoHTTPRead(pHTTPContext, cReadBuffer, iBufReadSize)) > 0)
    {
        // set return code
        *piRetCode = xmlNanoHTTPReturnCode(pHTTPContext);

        /* Maintain pointer to old location, free on failure */
        pInBufferRef = pInBuffer;

        pInBuffer = g_try_realloc(pInBuffer, iCurrSize + iReadSize);

        if (!pInBuffer || *piRetCode != HTTP_STATUS_OK)
        {
            // failure
            cleanup(pHTTPContext, pContentType);

            g_free(pInBufferRef);

            return pInBuffer; // it's NULL
        }

        memcpy(pInBuffer + iCurrSize, cReadBuffer, iReadSize);

        iCurrSize += iReadSize;

        // clear read buffer
        bzero(cReadBuffer, iBufReadSize);

        *piDataSize = iCurrSize;
    }

    if (iReadSize < 0)
    {
        // error
        g_free(pInBuffer);

        pInBuffer = NULL;
    }
    else
    {
        /* Maintain pointer to old location, free on failure */
        pInBufferRef = pInBuffer;

        // need to add '\0' at the end
        pInBuffer = g_try_realloc(pInBuffer, iCurrSize + 1);

        if (!pInBuffer)
        {
            // failure
            g_free(pInBufferRef);

            pInBuffer = NULL;
        }
        else
        {
            memcpy(pInBuffer + iCurrSize, "\0", 1);
        }
    }

    // finish up
    cleanup(pHTTPContext, pContentType);

    return pInBuffer;
}