static BaseType_t prvOpenUrl( xHTTPClient *pxClient ) { BaseType_t xRc; char pcSlash[ 2 ]; pxClient->bits.ulFlags = 0; if( pxClient->pcUrlData[ 0 ] != '/' ) { /* Insert a slah before the file name */ pcSlash[ 0 ] = '/'; pcSlash[ 1 ] = '\0'; } else { /* The browser provided a starting '/' already */ pcSlash[ 0 ] = '\0'; } snprintf( pxClient->pcCurrentFilename, sizeof pxClient->pcCurrentFilename, "%s%s%s", pxClient->pcRootDir, pcSlash, pxClient->pcUrlData); pxClient->pxFileHandle = ff_fopen( pxClient->pcCurrentFilename, "rb" ); FreeRTOS_printf( ( "Open file '%s': %s\n", pxClient->pcCurrentFilename, pxClient->pxFileHandle != NULL ? "Ok" : strerror( stdioGET_ERRNO() ) ) ); if( pxClient->pxFileHandle == NULL ) { xRc = prvSendReply( pxClient, WEB_NOT_FOUND ); /* "404 File not found" */ } else { pxClient->xBytesLeft = pxClient->pxFileHandle->ulFileSize; xRc = prvSendFile( pxClient ); } return xRc; }
BaseType_t xHTTPClientWork( xTCPClient *pxTCPClient ) { BaseType_t xRc; xHTTPClient *pxClient = ( xHTTPClient * ) pxTCPClient; if( pxClient->pxFileHandle != NULL ) { prvSendFile( pxClient ); } xRc = FreeRTOS_recv( pxClient->xSocket, ( void * )pcCOMMAND_BUFFER, sizeof( pcCOMMAND_BUFFER ), 0 ); if( xRc > 0 ) { BaseType_t xIndex; const char *pcEndOfCmd; const struct xWEB_COMMAND *curCmd; char *pcBuffer = pcCOMMAND_BUFFER; if( xRc < ( BaseType_t ) sizeof( pcCOMMAND_BUFFER ) ) { pcBuffer[ xRc ] = '\0'; } while( xRc && ( pcBuffer[ xRc - 1 ] == 13 || pcBuffer[ xRc - 1 ] == 10 ) ) { pcBuffer[ --xRc ] = '\0'; } pcEndOfCmd = pcBuffer + xRc; curCmd = xWebCommands; pxClient->pcUrlData = pcBuffer; /* Pointing to "/index.html HTTP/1.1" */ pxClient->pcRestData = pcEmptyString; /* Pointing to "HTTP/1.1" */ // Last entry is "ECMD_UNK" for( xIndex = 0; xIndex < WEB_CMD_COUNT - 1; xIndex++, curCmd++ ) { BaseType_t xLength; xLength = curCmd->xCommandLength; if( ( xRc >= xLength ) && ( memcmp( curCmd->pcCommandName, pcBuffer, xLength ) == 0 ) ) { char *pcLastPtr; pxClient->pcUrlData += xLength + 1; for( pcLastPtr = (char *)pxClient->pcUrlData; pcLastPtr < pcEndOfCmd; pcLastPtr++ ) { char ch = *pcLastPtr; if( ( ch == '\0' ) || ( strchr( "\n\r \t", ch ) != NULL ) ) { *pcLastPtr = '\0'; pxClient->pcRestData = pcLastPtr + 1; break; } } break; } } xRc = prvProcessCmd( pxClient, xIndex ); } else if( xRc < 0 ) { /* The connection will be closed and the client will be deleted */ FreeRTOS_printf( ( "xHTTPClientWork: rc = %ld\n", xRc ) ); } return xRc; }
static BaseType_t prvOpenURL( HTTPClient_t *pxClient ) { BaseType_t xRc; char pcSlash[ 2 ]; pxClient->bits.ulFlags = 0; #if( ipconfigHTTP_HAS_HANDLE_REQUEST_HOOK != 0 ) { if( strchr( pxClient->pcUrlData, ipconfigHTTP_REQUEST_CHARACTER ) != NULL ) { size_t xResult; xResult = uxApplicationHTTPHandleRequestHook( pxClient->pcUrlData, pxClient->pcCurrentFilename, sizeof( pxClient->pcCurrentFilename ) ); if( xResult > 0 ) { strcpy( pxClient->pxParent->pcContentsType, "text/html" ); snprintf( pxClient->pxParent->pcExtraContents, sizeof( pxClient->pxParent->pcExtraContents ), "Content-Length: %d\r\n", ( int ) xResult ); xRc = prvSendReply( pxClient, WEB_REPLY_OK ); /* "Requested file action OK" */ if( xRc > 0 ) { xRc = FreeRTOS_send( pxClient->xSocket, pxClient->pcCurrentFilename, xResult, 0 ); } /* Although against the coding standard of FreeRTOS, a return is done here to simplify this conditional code. */ return xRc; } } } #endif /* ipconfigHTTP_HAS_HANDLE_REQUEST_HOOK */ if( pxClient->pcUrlData[ 0 ] != '/' ) { /* Insert a slash before the file name. */ pcSlash[ 0 ] = '/'; pcSlash[ 1 ] = '\0'; } else { /* The browser provided a starting '/' already. */ pcSlash[ 0 ] = '\0'; } snprintf( pxClient->pcCurrentFilename, sizeof( pxClient->pcCurrentFilename ), "%s%s%s", pxClient->pcRootDir, pcSlash, pxClient->pcUrlData); pxClient->pxFileHandle = ff_fopen( pxClient->pcCurrentFilename, "rb" ); FreeRTOS_printf( ( "Open file '%s': %s\n", pxClient->pcCurrentFilename, pxClient->pxFileHandle != NULL ? "Ok" : strerror( stdioGET_ERRNO() ) ) ); if( pxClient->pxFileHandle == NULL ) { /* "404 File not found". */ xRc = prvSendReply( pxClient, WEB_NOT_FOUND ); } else { pxClient->uxBytesLeft = ( size_t ) pxClient->pxFileHandle->ulFileSize; xRc = prvSendFile( pxClient ); } return xRc; }