Friday, February 11, 2005
Embedded Ethernet and Internet Complete : I read the Japanese translated book to know how to development HTTP server and client to embedded systems. However it is so no good for me, because the book is based on commercial HTTP libraries such as Rabbit and TINI. |
However I am looking forward to create the network protocol stack for embedded system, because I am writing source codes only using C to after a long time. I am reading my old source code for Japanese computers or Macintosh System7 about ten years ago. It is fun work. I will create the library with the following programming style.
void HttpRequestRecieved(CgHttpRequest *httpReq)
{
char *uri = cg_http_request_geturi(httpReq);
CgHttpResponse *httpRes = cg_http_response_new();
cg_http_response_setstatuscode(httpRes, CG_HTTP_STATUS_OK);
cg_http_response_setcontent(httpRes, HTTP_SERVER_CONTENT);
cg_http_response_setcontentlength(httpRes, strlen(HTTP_SERVER_CONTENT));
cg_http_request_postresponse(httpReq, httpRes);
cg_http_response_delete(httpRes);
}
void ClinkCTestCase::testHttpServer()
{
/* get host interfaces */
CgNetworkInterfaceList *netIfList = cg_net_interfacelist_new();
int netIfCnt = cg_net_gethostinterfaces(netIfList);
CPPUNIT_ASSERT(0 < netIfCnt);
CgNetworkInterface *netIf = cg_net_interfacelist_getinterfaces(netIfList);
char *ipaddr = cg_net_interface_getaddress(netIf);
/* start http server */
CgHttpServer *httpServer = cg_http_server_new();
CPPUNIT_ASSERT(cg_http_server_open(httpServer, HTTP_SERVER_TEST_PORT, ipaddr) == TRUE);
cg_http_server_setlistener(httpServer, HttpRequestRecieved);
cg_http_server_start(httpServer);
/* test client */
cg_wait(1000);
CgHttpRequest *httpReq;
CgHttpResponse *httpRes;
httpReq = cg_http_request_new();
cg_http_request_setmethod(httpReq, CG_HTTP_GET);
cg_http_request_seturi(httpReq, "/index.html");
httpRes = cg_http_request_post(httpReq, ipaddr, HTTP_SERVER_TEST_PORT);
CPPUNIT_ASSERT(httpRes != NULL);
int statusCode = cg_http_response_getstatuscode(httpRes);
CPPUNIT_ASSERT(statusCode == 200);
int headerCnt = 0;
CgHttpHeader *header = cg_http_response_getheaders(httpRes);
for (;header; header=cg_http_header_next(header)) {
char *name = cg_http_header_getname(header);
char *value = cg_http_header_getvalue(header);
headerCnt++;
}
CPPUNIT_ASSERT(0 < headerCnt);
cg_http_request_delete(httpReq);
/* close http server */
cg_http_server_stop(httpServer);
cg_http_server_close(httpServer);
cg_http_server_delete(httpServer);
/* delete network interface list */
cg_net_interfacelist_delete(netIfList);
}