/*----------------------------------------------------------------------* rtp_wfile_open *----------------------------------------------------------------------*/ int rtp_wfile_open (RTP_HANDLE * fdPtr, const unsigned short * name, unsigned short flag, unsigned short mode) { unsigned char realName[32]; map_unicode_to_ascii(realName, (unsigned char *) name); return(rtp_file_open (fdPtr, realName, flag, mode)); }
void rtp_log_open(void) { if (rtp_file_open( &debugfd, LOG_FILENAME, RTP_FILE_O_RDWR|RTP_FILE_O_CREAT |RTP_FILE_O_TRUNC|RTP_FILE_O_BINARY, RTP_FILE_S_IWRITE|RTP_FILE_S_IREAD)==0) { start_system_msec = rtp_get_system_msec (); debugfd_valid = 1; rtp_printf("Logging enabled logging to file:%s\n", LOG_FILENAME); } else { rtp_printf("Logging disabled could not open log file:%s\n", LOG_FILENAME); debugfd_valid = 0; } }
int rtplatform_open(char RTSMB_FAR * name, unsigned short flag, unsigned short mode) { int fd, rv; rv = rtp_file_open ((RTP_FILE *) &fd, name, flag, mode); if (rv < 0) { RTSMB_DEBUG_OUTPUT_STR ("rtplatform_open: Error on open is ", RTSMB_DEBUG_TYPE_ASCII); RTSMB_DEBUG_OUTPUT_INT (rv); RTSMB_DEBUG_OUTPUT_STR ("\n", RTSMB_DEBUG_TYPE_ASCII); return -1; } return fd; }
/** @memo Opens a file using the supplied name. @doc Opens a file using the mode settings, and sets the file information. @return 0 if successful, -1 on failure, and -2 if a non-fatal error occurred. */ int rtp_stdio_fopen ( void ** rtpfile, /** A pointer to the address of an RTP_FILE to store the file information. */ const char * fname, /** The name of the file to open. */ const char * mode /** For the mode argument:<br> <pre> | "r" Open a file for reading only RTP_FILE_O_RDONLY | "r+" Open a file for reading and writing RTP_FILE_O_RDWR | "w" Open a file for writing only RTP_FILE_O_WRONLY | Create a file if it does not exist RTP_FILE_O_CREAT | Truncate a file to 0 bytes after opening RTP_FILE_O_TRUNC | "w+" Open a file for reading and writing RTP_FILE_O_RDWR | Create a file if it does not exist RTP_FILE_O_CREAT | Truncate a file to 0 bytes after opening RTP_FILE_O_TRUNC | "a" All writes will be appended to the file RTP_FILE_O_APPEND | Create a file if it does not exist RTP_FILE_O_CREAT | "a+" Open a file for reading and writing RTP_FILE_O_RDWR | All writes will be appended to the file RTP_FILE_O_APPEND | Create a file if it does not exist RTP_FILE_O_CREAT | "b..." b prepended to the mode in the open call | causes the file to be opened in binary mode RTP_FILE_O_BINARY | "t..." t prepended to the mode in the open call | causes the file to be opened in text mode RTP_FILE_O_TEXT | Note: If neither the b or t are prepended, text | mode is the default implementation. </pre> */ ) { RTP_STDIO_FILE * p; int result; result = (-1); p = (RTP_STDIO_FILE *) rtp_malloc ((unsigned int) sizeof (*p), 0); /* SPRZ */ if (p) { rtp_memset(p, 0, sizeof(*p)); p->verify = RTP_FILE_VERIFICATION_VALUE; if ((result = rtp_file_open (&(p->handle), fname, _rtp_mode_to_permission(mode), RTP_FILE_S_IWRITE | RTP_FILE_S_IREAD)) != 0) { /* --------------------------------------- */ /* ERROR: drop the handle block. */ /* --------------------------------------- */ rtp_free ((void *)p); } } *rtpfile = (void *)p; return (result); }
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); } }
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); } }
/*---------------------------------------------------------------------------*/ 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); }