add header information http-server
Topic last updated
28 Mar 2011, by
Olle Kelderman.
10 replies
header,
http,
server
omkelderman
#
23 Mar 2011
Hi,
At this time I run an HTTP-server on my mbed using the library lwip.
Every time when I visit the web-page of my mbed, the mbed does something with the query-string. That works fine, but I need to access this web-page using an AJAX-request. And because my mbed is an other domain, I get an 'cross-domain-error'.
If it was an PHP-website, I could easily add this php-code:
php-code
header('Access-Control-Allow-Origin: *');
This allows the AJAX-request and there is no cross-domain-error.
But how can I do this on the mbed?? Is there a way to add header-information??
I hope there is someone who can help me...
Thank you,
Olle
Replies
omkelderman
#
26 Mar 2011
Sorry for the late reply, but suddenly I became ill.
It looks good, but I've no idea how to add this in de code I've already have... It's a code of 208 lines, so I don't know if it's a good idea to post it here...
you would have to put it inside your custome httprequesthandler routine
look at the link I gave, its the same format, you only have to rename your own handler. Read the example and it should give you the idea how to implement it
here's how mine looks like, I called it POSTHANDLER

omkelderman
#
26 Mar 2011
I'm afraid that I can't find the httprequesthandler. I understand that it must be somewhere, but I don't know where.
Maybe it helps to know that I use:
See http://mbed.org/users/no2chem/notebook/ethernet-testing/
For usage information.
(and add some stuff that do something I want, but that's not interesting)
As you can see this script generate an html-page and I want to add that header to that page.
EDIT: and by the way, it is an GET-request.
your code is very different from mine. I left all the TCP connection code handled by the HTTPRequestHandler
mine looks like
main.c
svr.bind(80);
svr.addHandler<PostHandler>("/android");
while (1){
Net::poll();
}
<<//code>>
in a seperate file PostHandler.cpp
<<code>>
PostHandler::PostHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket):HTTPRequestHandler(rootPath, path, pTCPSocket){
}
PostHandler::~PostHandler(){
}
void PostHandler::doGet(){
//this is where you process GET request
}
void PostHandler::doPost()
{
//this is where you process POST request
//get post variables
string _path = path();
string _rootpath = rootPath();
int _datalen = dataLen();
//grab header variables
string _content_length = reqHeaders()["Content-Length"];
string _content_type = reqHeaders()["Content-Type"];
string _user_agent = reqHeaders()["User-Agent"];
string _expect = reqHeaders()["Expect"];
}
void PostHandler::doHead(){
}
void PostHandler::onReadable(){
int _datalen = dataLen(); //read POST data length
//allocate size of readable data
char* _send_data = (char *) malloc(sizeof(char) * _datalen);
//read POST data
readData(_send_data, _datalen);
//process POST data here, you can call any subroutine from here
//write any response header
respHeaders()["Content-Type"] = "text/plain; charset=utf-8";
respHeaders()["Connection"] = "close";
//http_response contains any html code as my reply
writeData(http_response, strlen(http_response));
}
void PostHandler::onWriteable(){
close();
}
void PostHandler::onClose(){
return;
}
omkelderman
#
27 Mar 2011
I'm a beginner, so the only thing I do is searching for codes that already exists on this website en add some stuff to let it do what I want. I haven't the knowledge to write the code by myself.
If you say that your code can handle an GET-request en write some data back to the browser than I can use your code.
Is this the whole code, or is there something more (an library for example)? What is the code to make an connection with the router, so the mbed gets an IP-adres? (I'm sorry if I ask stupid questions, but I'm still a beginner...)
The thing I want to do sounds actually very simple: I want to use my MBED as an web-server (whit his own IP-adres) and I can do an GET-request (using AJAX) on de root-page of that webserver and the MBED needs the query-variables and if it succeeds the MBED have to write an succeed-massage back so the AJAX-request can read it and see that it is succeeded.
I can do this with what I have now, accept for adding that header, so I can't do the AJAX-request.
It's what I use on my program. Its basically a web server handling POST request from clients and sending response
if you look at this section
void PostHandler::onReadable(){
int _datalen = dataLen(); //read POST data length
//allocate size of readable data
char* _send_data = (char *) malloc(sizeof(char) * _datalen);
//read POST data
readData(_send_data, _datalen);
//process POST data here, you can call any subroutine from here
//write any response header here that you deem useful for your application
respHeaders()["Content-Type"] = "text/plain; charset=utf-8";
respHeaders()["Connection"] = "close";
//http_response contains any html code as my reply
//the html_response you see here is my reply based on the POST request the client sent. You can populate this with whatever you want
writeData(http_response, strlen(http_response));
}
omkelderman
#
27 Mar 2011
Thanks. I have to do something else now, but later today I'll take a look.
omkelderman
#
28 Mar 2011
I've tried something, but it doesn't work. What is the complete code that you use??
Please log in to post a reply.
Hi,
At this time I run an HTTP-server on my mbed using the library lwip. Every time when I visit the web-page of my mbed, the mbed does something with the query-string. That works fine, but I need to access this web-page using an AJAX-request. And because my mbed is an other domain, I get an 'cross-domain-error'. If it was an PHP-website, I could easily add this php-code:
php-code
header('Access-Control-Allow-Origin: *');This allows the AJAX-request and there is no cross-domain-error.
But how can I do this on the mbed?? Is there a way to add header-information??
I hope there is someone who can help me...
Thank you, Olle