Exemple #1
0
/** @memo   Write a buffer to a file.

    @doc    Write n items of size len from the buffer to the current
    position in the file.
    
    @precondition Should not mix rtp_stdio_fwrite() and 
    rtp_stdio_fgets() calls.
    
    @return Number of items written, -1 on error, and -2 on non-fatal 
    error.
 */
long rtp_stdio_fwrite (
  void * buffer,                        /** Buffer to write. */
  unsigned long len,                    /** Size of items to write. */
  unsigned long n,                      /** Number of items to write. */
  void * rtpfile                        /** Handle of the file to be written to. */
  )
{
int result = (-1);

    if( !rtpfile || (((RTP_STDIO_FILE *)rtpfile)->verify != RTP_FILE_VERIFICATION_VALUE) )
    {
        /* --------------------------------------- */
        /*  Application passed an RTP_HANDLE       */
        /*  instead of an RTP_STDIO_FILE. Should   */
		/*  use low level rtp_file_write().        */
        /* --------------------------------------- */
        return ((long) -1);
    }

    if (((RTP_STDIO_FILE *)rtpfile)->handle)
    {
        result = rtp_file_write (((RTP_STDIO_FILE *)rtpfile)->handle, (unsigned char *) buffer, (unsigned int) (len * n));
        if (result > 0)
        {
            result /= len;
        }
    }
    return ((long) result);
}
void rtp_log_write(char *prompt, void *pData, long nBytes)
{
    if (debugfd_valid)
    {
        unsigned long elapsed;
        char outbuff[256];
        elapsed = rtp_get_system_msec () - start_system_msec;
        rtp_sprintf(outbuff,"\r\n ==<%s (t=%8d) (c=%4d) >== \r\n", (const unsigned char*) prompt,elapsed, nBytes);
        rtp_file_write (debugfd, (const unsigned char*) outbuff, (long) rtp_strlen(outbuff));
        if (pData && nBytes > 0)
        {
            rtp_file_write (debugfd, (const unsigned char*) pData, (long) nBytes);
            rtp_sprintf(outbuff,"\r\n ==<===========================================>==\r\n");
            rtp_file_write (debugfd, (const unsigned char*) outbuff, (long) rtp_strlen(outbuff));
        }
        rtp_file_flush (debugfd);
    }
}
Exemple #3
0
long rtplatform_write(int fd,  unsigned char RTSMB_FAR * buf, long count)
{
    long rv;


    rv = rtp_file_write ((RTP_HANDLE) fd, buf, count);

    if (rv < 0)
    {
        return -1;
    }
    return rv;
}
Exemple #4
0
static int http_client_get(HTTPManagedClient* phttpClient, int savetofile)
{
HTTPManagedClientSession* session = 0;
char urlpath[255];
char urlfile[255];
char localfile[255];
RTP_HANDLE  fd;

HTTP_INT32 result = -1;
HTTP_INT32 totalresult = 0;
HTTPResponseInfo info;

	rtp_gui_prompt_text(" ","IP Address (eg: 192.161.2.1) or domain name of host (eg: www.google.com)\n    :", &urlpath[0]);
	rtp_gui_prompt_text(" ","File name: (eg: /index.html or / or /mydownloads/mypdf.pdf\n    :", &urlfile[0]);

	if (savetofile)
	{
		rtp_gui_prompt_text(" ","Local file name: \n    :", &localfile[0]);
		if (rtp_file_open (&fd, (const char *) &localfile[0], (RTP_FILE_O_CREAT|RTP_FILE_O_RDWR|RTP_FILE_O_TRUNC), RTP_FILE_S_IWRITE) < 0)
		{
	 		rtp_printf("Failure opening %s\n", localfile);
	 		return(-1);
		}
	}


	/* A HTTPManagedClientSession is the abstraction for a SINGLE HTTP request/response pair.
	Thus a new session must be opened for each HTTP operation
	(although this may not cause a new connection to be established, if keep alive is used),
	and closed when the operation has completed (though, again, this may not actually close a
	physical network connection) */

	if (HTTP_ManagedClientStartTransaction (
	 phttpClient,
	 &urlpath[0],
	 80,
      4,
	 HTTP_SESSION_TYPE_TCP,
	 1, /* blocking? */ &session ) < 0)
	{
	 	rtp_printf("Failed: retrieving data from from %s/%s\n", urlpath, urlfile);
	 	return(-1);
	}


	 /* Once the session is open, one command may be issued; in this case a GET
	   (by calling HTTP_ManagedClientGet) */
	 HTTP_ManagedClientGet(session, urlfile, 0 /* if-modified-since */);

	 /* This may be called at any time after the command is issued to get information
	    about the server response; this info includes the status code given, date when
	    the request was processed, file mime type information (if a file is transferred
	    as the result of a command), authentication information, etc. */

	 HTTP_ManagedClientReadResponseInfo(session, &info);

	 do {
	 	/* Read data from the session */
	 	result = HTTP_ManagedClientRead(session, read_buffer, read_buffer_size);
		if (result > 0)
		{
			totalresult += result;
			if (savetofile)
			{
				if (rtp_file_write(fd,read_buffer, (long)read_buffer_size) < 0)
				{
					rtp_printf("Failed writing to file\n");
					return(-1);
				}
			}
		}
	 } while (result > 0);

	/* Now we are done; close the session (see note above about sessions) */
	 HTTP_ManagedClientCloseSession(session);

	 /* When all HTTP client activity is completed, the managed client context may safely be destroyed */
	 HTTP_ManagedClientDestroy(phttpClient);


	if (savetofile)
	{
		rtp_file_close(fd);
	}
	 if (result == 0)
	 {
	 	rtp_printf("Success: (%d) bytes retrieved from %s/%s\n", totalresult, urlpath, urlfile);
	 	return(0);
	 }
	 else
	 {
	 	rtp_printf("Failure: (%d) bytes retrieved from %s/%s\n", totalresult, urlpath, urlfile);
	 	return(-1);
	 }
}