How to Use httpd_sendfile

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Use httpd_sendfile

967 Views
M_ttferrari
Contributor III

Hi,

 

Can someone help me with this problem? I want to send a text file (file.txt) using httpd_sendfile. 

 

I Supouse that I need some like cgi callback funtion or somethig similar, but I'm no sure abuot it neither how to use this funtion. 

 

In http_supp.h is the prototype of httpd_sendfile that has as parameters:

HTTP_STRUCT *server

HTTP_SESSION_STRUCT *session

 

Where does file.txt appear? Does the server response to http_request.open('GET', 'http://webs/file.txt', true); is a file in a  callback funtion? How to do I handle this request?

 

I hope that my questions were understood and exposed in a clearly way 

Thank you in advance

Labels (1)
Tags (1)
0 Kudos
2 Replies

423 Views
admin
Specialist II

Hello.

 

I am using the Freescale MCF52259 microcontroller with MQX and I am experimenting a similar issue.

 

I have a .CGI callback function configured in the httpd server. When a .CGI GET request from a web client is sent to the httpd server the callback function associated is executed as expected. Until here everything works fine.

 

The problem is that I need to send a piece of a file stored on an external storage device (Micro SD Card) connected to the microcontroller as an XML string when the GET request is issued and the callback CGI function is executed.

 

I have installed a file system for the Micro SD card (a: ) and I can access the files already (read/write).

 

But When I use the MQX generic functions (fopen, fseek, fclose, ioctl) inside the callback CGI function to access the files of Micro SD card and send the data as an string an exception occurs (Exception vector name: Address error) while debugging.

 

My workaround was that I had to implement a temporary Micro SD Card data access system that is executed on a different task that has priority 1 and is set to blocked by default. When the callback CGI function is executed it sets that task to ready and wait until data from MS Card is read and stored in a temporary common buffer for both the cgi callback function and the MSD card data access task. The task is allways set back to blocked once it finishes executing the MSD card data access proccess.

 

This works fine for me but consumes a lot of RAM memory resources getting to 98% of usage while executed.

 

1. I would like to know why the Address error Exception happens when I use the MQX generic functions (fopen, fseek, fclose, ioctl) inside the callback CGI function to access files stored on Micro SD Card.

 

2. Is it possible to use httpd_sendfile?

 

3. What is the best way to read data from file (stored on Micro SD card file system) and send it as an string when a CGI callback function is executed?

 

Any help or advice will be very appreciated.

 

Thank you so much and my best regards.

0 Kudos

423 Views
madifazio
Contributor III

Hello Alejandro

 

We actually use this code to send a file line by line

 

static int cgi_registrosOnline(HTTPD_SESSION_STRUCT *session){  char *buffer = mram_alloc(100);  session->response.file = fopen("a:/registro/historia.txt","r"); if(session->response.file != NULL){   while(fgets(buffer,95,session->response.file) != NULL){   strcat(buffer,"\n");   httpd_sendstr(session->sock,buffer);  }  fclose(session->response.file); }  mram_free(buffer); return session->request.content_len;}

 

mram_alloc and mram_free are functions we use to encapsulate memory request from a pool that is located in external mram memory.

 

0 Kudos