Example #1
0
/** @memo   Close a file.

    @doc    Closes a file that was opened with rtp_stdio_fopen.

    @return 0 if successful, -1 otherwise.
 */
int rtp_stdio_fclose (
  void * rtpfile                        /** File handle used to close the file. */
  )
{
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_close().        */
        /* --------------------------------------- */
        return (-1);
    }

    result = rtp_file_close (((RTP_STDIO_FILE *)rtpfile)->handle);
    if (((RTP_STDIO_FILE *)rtpfile)->buffer)
    {
        rtp_free ((void *)((RTP_STDIO_FILE *)rtpfile)->buffer);
    }
    rtp_free (rtpfile);
    rtpfile = (void *) 0;
    return (result);
}
Example #2
0
void rtp_log_close(void)
{
	if (debugfd_valid)
	{
		rtp_file_close(debugfd);
	}
}
Example #3
0
int rtplatform_close(int fd)
{
    int rv;

    rv = rtp_file_close ((RTP_FILE) fd);
    if (rv < 0)
    {
        return -1;
    }
    return rv;
}
Example #4
0
static int http_client_put(HTTPManagedClient* phttpClient)
{
HTTPManagedClientSession* session = 0;
char urlpath[255];
char urlfile[255];
char localfile[255];
char contenttype[255];
HTTP_INT32 result = -1;
HTTP_INT32 totalresult = 0;
HTTPResponseInfo info;
HTTP_INT32 contentLength;
RTP_HANDLE  fd;
HTTP_INT32 nread;

	rtp_strcpy(contenttype, "application/x-www-form-urlencoded"); /* content-type */

	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 on server\n    :", &urlfile[0]);
	rtp_gui_prompt_text(" ","Local file name to put\n    :", &localfile[0]);
	rtp_gui_prompt_text(" ","Content type eg: text/html\n    :", &contenttype[0]);

	contentLength = 0;			/* Set content length to zero so we use chunked encoding */

	/* 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: connecting to %s\n", urlpath);
	 	return(-1);
	}


	/* Once the session is open, one command may be issued; in this case a Post */
 	HTTP_ManagedClientPut ( session,
 	urlfile, /* path */
 	contenttype,
 	contentLength /* content-length */ );

	if (rtp_file_open (&fd, (const char *) &localfile[0], RTP_FILE_O_RDONLY, 0) < 0)
	{
 		rtp_printf("Failure opening %s\n", localfile);
 		return(-1);
	}

 	/* write the POST data */
	do
	{

 		nread = rtp_file_read(fd,read_buffer, (long)read_buffer_size);
 		if (nread > 0)
			HTTP_ManagedClientWrite (session, (HTTP_UINT8*) &read_buffer[0], nread);
	} while (nread >= 0);

/* this function must be called when all data has been written */
	HTTP_ManagedClientWriteDone (session);

/* 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);
   } 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);

   rtp_file_close(fd);

   if (result == 0)
   {
   		rtp_printf("Success: putting file: %s to %s%s\n", localfile, urlpath, urlfile);
   		return(0);
	}
	else
	{
   		rtp_printf("Failed: putting file: %s to %s%s\n", localfile, urlpath, urlfile);
		return(-1);
	}
}
Example #5
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);
	 }
}
Example #6
0
/*---------------------------------------------------------------------------*/
static int __HTTP_ServerHandleRequest (HTTPServerRequestContext *ctx,
							   HTTPSession *session,
                               HTTPRequest *request,
                               RTP_NET_ADDR *clientAddr,
                               HTTPVirtualFileInfo *vfile)
{
	HTTP_CHAR fsPath[HTTP_SERVER_PATH_LEN];
	HTTPServerContext *server = ctx->server;

	switch (request->methodType)
	{
		case HTTP_METHOD_GET:
		{
			HTTPResponse response;
			RTP_FILE fd;
			HTTP_INT32 bytesRead, bufferSize = 1024;
			HTTP_INT32 bytesleft=0;
			HTTP_UINT8 buffer[1024];
			FileContentType contentType;
			unsigned chunked = 0;
			unsigned found = 0;
            const HTTP_UINT8 *pdata;

            if (vfile)
            {
                pdata=vfile->data;
                bytesleft = vfile->size;
            }
            if (vfile)
            {
			    HTTP_ServerReadHeaders(
					ctx,
					session,
					 _HTTP_VirtualFileProcessHeader,
					 0,
					 buffer,
					 bufferSize
				);
                found = 1;
            }
            else
            {
			    _HTTP_ServerReadHeaders(ctx, session, 0, 0, buffer, bufferSize);
			    _HTTP_ConstructLocalPath(server, fsPath, (HTTP_CHAR *) request->target, HTTP_SERVER_PATH_LEN);
			    if (rtp_file_open(&fd, fsPath, RTP_FILE_O_BINARY|RTP_FILE_O_RDONLY, RTP_FILE_S_IREAD) < 0)
			    {
				    if ((server->defaultFile) && (server->defaultFile[0]) &&
				        (fsPath[0]=='\0'||(fsPath[rtp_strlen(fsPath)-1] == '\\') || (fsPath[rtp_strlen(fsPath)-1] == '/')))
					{
					    if ((HTTP_SERVER_PATH_LEN - rtp_strlen(server->defaultFile)) > rtp_strlen(server->defaultFile))
					    {
						    rtp_strcat(fsPath, server->defaultFile);
						    if (rtp_file_open(&fd, fsPath, RTP_FILE_O_BINARY|RTP_FILE_O_RDONLY, RTP_FILE_S_IREAD) >= 0)
						    {
							    found = 1;
						    }
					    }
					}
				}
				else
					found = 1;
			}
			if (!found)
			{
				_HTTP_ServerSendError(ctx, session, 404, "Not Found");
				return (0);
			}

			_HTTP_ServerInitResponse(ctx, session, &response, 200, "OK");
			_HTTP_ServerSetDefaultHeaders(ctx, &response);

            if (vfile)
            {
			    HTTP_SetResponseHeaderStr(&response, "Content-Type", vfile->contentType);
			    if (vfile->contentEncoding)
			    {
				    HTTP_SetResponseHeaderStr(&response, "Content-Encoding", vfile->contentEncoding);
			    }
			    HTTP_SetResponseHeaderTime(&response, "Last-Modified", &vfile->creationTime);
            }
            else
            {
			    contentType = GetFileTypeByExtension(fsPath);
			    if (contentType != FILE_TYPE_UNKNOWN)
			    {
				    HTTP_SetResponseHeaderStr(&response, "Content-Type", FileContentTypeToStr(contentType));
			    }
            }

			if (ctx->keepAlive)
			{
				if (request->httpMajorVersion > 1 || (request->httpMajorVersion == 1 && request->httpMinorVersion >= 1))
				{
					HTTP_SetResponseHeaderStr(&response, "Transfer-Encoding", "chunked");
					chunked = 1;
				}
				else
				{
					void* dirobj;
					unsigned long fileSize;
					if (vfile)
                    {
                        fileSize = vfile->size;
                    }
                    else
                    {
					    if (rtp_file_gfirst(&dirobj, fsPath) < 0)
					    {
						    _HTTP_ServerSendError(ctx, session, 500, "Internal Server Error");
						    HTTP_FreeResponse(&response);
						    rtp_file_close(fd);
						    return (-1);
					    }
					    if (rtp_file_get_size(dirobj, &fileSize) < 0)
					    {
						    _HTTP_ServerSendError(ctx, session, 500, "Internal Server Error");
						    HTTP_FreeResponse(&response);
						    rtp_file_gdone(dirobj);
						    rtp_file_close(fd);
						    return (-1);
					    }
						rtp_file_gdone(dirobj);
                    }
				    HTTP_SetResponseHeaderInt(&response, "Content-Length", fileSize);
				}
			}
			HTTP_WriteResponse(session, &response);
			HTTP_FreeResponse(&response);

			do
			{
#define EBSMIN(X,Y)    ((X)<(Y)?(X):(Y))
                if (vfile)
                {
                    bytesRead=EBSMIN(bufferSize,bytesleft);
                    rtp_memcpy(buffer,pdata,bytesRead);
                    bytesleft -= bytesRead;
					pdata += bytesRead;
                }
                else
				    bytesRead = rtp_file_read(fd, buffer, bufferSize);

				if (bytesRead > 0)
				{
					if (chunked)
					{
						if (HTTP_WriteChunkStart(session, bytesRead) < 0)
						{
							break;
						}
					}

					if (HTTP_Write(session, buffer, bytesRead) < bytesRead)
					{
						break;
					}

					if (chunked)
					{
						if (HTTP_WriteChunkEnd(session) < 0)
						{
							break;
						}
					}
				}

			} while (bytesRead > 0);

			/* this tells the client the transfer is complete */
			if (chunked)
			{
				HTTP_WriteChunkStart(session, 0);
				HTTP_WriteChunkEnd(session);
			}

			HTTP_WriteFlush(session);
            if (!vfile)
			    rtp_file_close(fd);

			break;
		}

		default:
			break;
	}

	return (0);
}