lib/sw_apps/img_rcvry/src/xbir_http.c
static int Xbir_HttpProcessGetFileReq (struct tcp_pcb *Tpcb, u8 *HttpReq,
	u16 HttpReqLen)
{
	int Status = XST_FAILURE;
	int Error;
	char FileName[XBIR_HTTP_MAX_FILE_NAME_LEN] = "";
	char Buffer[XBIR_HTTP_BUFFER_SIZE];
	FIL Fil;
	const char *FileExt;
	u32 HdrLen;
	u32 FileSize;
	u32 DataReadLen;
	FRESULT Result;

	Xbir_HttpExtractFileName((char *)HttpReq, HttpReqLen, FileName,
		XBIR_HTTP_MAX_FILE_NAME_LEN - 1U);

	/* Check if specified file present in web server's FAT file system */
	Result = f_open(&Fil, FileName, FA_READ);
	if (Result) {
		Xbir_Printf("Error: Requested file %s not found, returning 404\r\n",
			   FileName);
		Xbir_HttpSendResponse404(Tpcb, HttpReq, HttpReqLen);
		goto END;
	}

	Xbir_Printf("\r\n HTTP GET: %s\r\n", FileName);
	FileExt = Xbir_UtilGetFileExt(FileName);
	FileSize = f_size(&Fil);

	/* Send the HTTP headers */
	HdrLen = Xbir_HttpGenerateHdr(Buffer, FileExt, FileSize);
	Error = tcp_write(Tpcb, Buffer, HdrLen,
		TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
	if (Error != ERR_OK) {
		Xbir_Printf("ERROR: (%d) writing http header to socket\r\n",
				Error);
		Xbir_Printf("attempted to write #bytes = %d, tcp_sndbuf = %d\r\n",
			HdrLen, tcp_sndbuf(Tpcb));
		Xbir_Printf("HTTP header = %s\r\n", Buffer);
		f_close(&Fil);
		Xbir_HttpClose(Tpcb);
		goto END;
	}

	/* Now send the file */
	while (FileSize > 0U) {
		int SndBuffer = tcp_sndbuf(Tpcb);
		if (SndBuffer < XBIR_HTTP_BUFFER_SIZE) {
			/* Not enough space in sndbuf, so send remaining bytes
			 * when there is space. this is done by storing the file
			 * descriptor in as part of the tcp_arg, so that the sent
			 * callback handler knows to send data
			 */
			Xbir_HttpArg *HttpArg = (Xbir_HttpArg *)Tpcb->callback_arg;
			memcpy(&HttpArg->Fil, &Fil, sizeof(Fil));
			HttpArg->Fsize = FileSize;
			goto END;
		}

		f_read(&Fil, (void *)Buffer, XBIR_HTTP_BUFFER_SIZE, &DataReadLen);
		Error = tcp_write(Tpcb, Buffer, DataReadLen,
			TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
		if (Error != ERR_OK) {
			Xbir_Printf("Error: writing file (%s) to socket,"
				"remaining unwritten bytes = %d\r\n",
				FileName, FileSize - DataReadLen);
			Xbir_Printf("Attempted to lwip_write %d bytes,"
				" tcp write error = %d\r\n", DataReadLen, Error);
			Xbir_HttpClose(Tpcb);
			break;
		}

		FileSize -= DataReadLen;
	}

	f_close(&Fil);
	Status = XST_SUCCESS;

END:
	return Status;
}
/*****************************************************************************/
/**
 * @brief
 * This function processes HTTP GET command for accessing web server files.
 * It initiates file transfer if request is ligitimate, else sends HTTP 404
 * error.
 *
 * @param	Tpcb		Pointer to TCP PCB
 * @param	HttpReq		Pointer to HTTP payload
 * @param	HttpReqLen	HTTP payload length
 *
 * @return	XST_SUCCESS if request is processed successfully
 *		XST_FAILURE if request is not processed
 *
 *****************************************************************************/
@brief
This function processes HTTP GET command for accessing web server files.
It initiates file transfer if request is ligitimate, else sends HTTP 404
error.

@param       Tpcb            Pointer to TCP PCB
@param       HttpReq         Pointer to HTTP payload
@param       HttpReqLen      HTTP payload length

@return      XST_SUCCESS if request is processed successfully
XST_FAILURE if request is not processed
@brief This function processes HTTP GET command for accessing web server files . It initiates file transfer if request is ligitimate else sends HTTP 404 error . @param Tpcb Pointer to TCP PCB @param HttpReq Pointer to HTTP payload @param HttpReqLen HTTP payload length @return XST_SUCCESS if request is processed successfully XST_FAILURE if request is not processed
static int Xbir_HttpProcessGetFileReq (struct tcp_pcb *Tpcb, u8 *HttpReq,
	u16 HttpReqLen)
{
	int Status = XST_FAILURE;
	int Error;
	char FileName[XBIR_HTTP_MAX_FILE_NAME_LEN] = "";
	char Buffer[XBIR_HTTP_BUFFER_SIZE];
	FIL Fil;
	const char *FileExt;
	u32 HdrLen;
	u32 FileSize;
	u32 DataReadLen;
	FRESULT Result;
	Xbir_HttpExtractFileName((char *)HttpReq, HttpReqLen, FileName,
		XBIR_HTTP_MAX_FILE_NAME_LEN - 1U);
	Result = f_open(&Fil, FileName, FA_READ);
	if (Result) {
		Xbir_Printf("Error: Requested file %s not found, returning 404\r\n",
			   FileName);
		Xbir_HttpSendResponse404(Tpcb, HttpReq, HttpReqLen);
		goto END;
	}
	Xbir_Printf("\r\n HTTP GET: %s\r\n", FileName);
	FileExt = Xbir_UtilGetFileExt(FileName);
	FileSize = f_size(&Fil);
	HdrLen = Xbir_HttpGenerateHdr(Buffer, FileExt, FileSize);
	Error = tcp_write(Tpcb, Buffer, HdrLen,
		TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
	if (Error != ERR_OK) {
		Xbir_Printf("ERROR: (%d) writing http header to socket\r\n",
				Error);
		Xbir_Printf("attempted to write #bytes = %d, tcp_sndbuf = %d\r\n",
			HdrLen, tcp_sndbuf(Tpcb));
		Xbir_Printf("HTTP header = %s\r\n", Buffer);
		f_close(&Fil);
		Xbir_HttpClose(Tpcb);
		goto END;
	}
	while (FileSize > 0U) {
		int SndBuffer = tcp_sndbuf(Tpcb);
		if (SndBuffer < XBIR_HTTP_BUFFER_SIZE) {
			Xbir_HttpArg *HttpArg = (Xbir_HttpArg *)Tpcb->callback_arg;
			memcpy(&HttpArg->Fil, &Fil, sizeof(Fil));
			HttpArg->Fsize = FileSize;
			goto END;
		}
		f_read(&Fil, (void *)Buffer, XBIR_HTTP_BUFFER_SIZE, &DataReadLen);
		Error = tcp_write(Tpcb, Buffer, DataReadLen,
			TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE);
		if (Error != ERR_OK) {
			Xbir_Printf("Error: writing file (%s) to socket,"
				"remaining unwritten bytes = %d\r\n",
				FileName, FileSize - DataReadLen);
			Xbir_Printf("Attempted to lwip_write %d bytes,"
				" tcp write error = %d\r\n", DataReadLen, Error);
			Xbir_HttpClose(Tpcb);
			break;
		}
		FileSize -= DataReadLen;
	}
	f_close(&Fil);
	Status = XST_SUCCESS;
END:
	return Status;
}
static int Xbir_HttpProcessGetFileReq ( struct tcp_pcb * Tpcb , u8 * HttpReq , u16 HttpReqLen ) { int Status = XST_FAILURE ; int Error ; char FileName [ XBIR_HTTP_MAX_FILE_NAME_LEN ] = " " ; char Buffer [ XBIR_HTTP_BUFFER_SIZE ] ; FIL Fil ; const char * FileExt ; u32 HdrLen ; u32 FileSize ; u32 DataReadLen ; FRESULT Result ; Xbir_HttpExtractFileName ( ( char * ) HttpReq , HttpReqLen , FileName , XBIR_HTTP_MAX_FILE_NAME_LEN - 1U ) ; Result = f_open ( & Fil , FileName , FA_READ ) ; if ( Result ) { Xbir_Printf ( " \r \n " , FileName ) ; Xbir_HttpSendResponse404 ( Tpcb , HttpReq , HttpReqLen ) ; goto END ; } Xbir_Printf ( " \r \n \r \n " , FileName ) ; FileExt = Xbir_UtilGetFileExt ( FileName ) ; FileSize = f_size ( & Fil ) ; HdrLen = Xbir_HttpGenerateHdr ( Buffer , FileExt , FileSize ) ; Error = tcp_write ( Tpcb , Buffer , HdrLen , TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE ) ; if ( Error != ERR_OK ) { Xbir_Printf ( " \r \n " , Error ) ; Xbir_Printf ( " \r \n " , HdrLen , tcp_sndbuf ( Tpcb ) ) ; Xbir_Printf ( " \r \n " , Buffer ) ; f_close ( & Fil ) ; Xbir_HttpClose ( Tpcb ) ; goto END ; } while ( FileSize > 0U ) { int SndBuffer = tcp_sndbuf ( Tpcb ) ; if ( SndBuffer < XBIR_HTTP_BUFFER_SIZE ) { Xbir_HttpArg * HttpArg = ( Xbir_HttpArg * ) Tpcb -> callback_arg ; memcpy ( & HttpArg -> Fil , & Fil , sizeof ( Fil ) ) ; HttpArg -> Fsize = FileSize ; goto END ; } f_read ( & Fil , ( void * ) Buffer , XBIR_HTTP_BUFFER_SIZE , & DataReadLen ) ; Error = tcp_write ( Tpcb , Buffer , DataReadLen , TCP_WRITE_FLAG_COPY | TCP_WRITE_FLAG_MORE ) ; if ( Error != ERR_OK ) { Xbir_Printf ( " " " \r \n " , FileName , FileSize - DataReadLen ) ; Xbir_Printf ( " " " \r \n " , DataReadLen , Error ) ; Xbir_HttpClose ( Tpcb ) ; break ; } FileSize -= DataReadLen ; } f_close ( & Fil ) ; Status = XST_SUCCESS ; END : return Status ; }
@brief
 This function processes HTTP GET command for accessing web server files.
@brief This function processes HTTP GET command for accessing web server files .
/* Check if specified file present in web server's FAT file system */ /* Send the HTTP headers */ /* Now send the file */ /* Not enough space in sndbuf, so send remaining bytes
			 * when there is space. this is done by storing the file
			 * descriptor in as part of the tcp_arg, so that the sent
			 * callback handler knows to send data
			 */