Usage
Http Client
First declare a Http Client FUB of type LLHttpClient called client. Then call the FUB cyclically until the ‘connected’ output goes TRUE: this means that the initial connection with the server has succeeded, and that other blocks from the library can now be called using the ‘ident’ output from this block.
void _INIT ProgramInit(void)
{
client.enable = 1;
client.bufferSize = 10000;
strcpy(client.hostname, "google.com");
client.port = 80;
client.localPort = 1237;
request.method = LLHTTP_METHOD_GET;
strcpy(request.uri, "/");
request.pResponse = &responseMessage;
request.responseSize = sizeof(responseMessage);
}
void _CYCLIC ProgramCyclic(void)
{
LLHttpClient(&client);
request.ident = client.ident;
request.send = client.connected; // Once we connected, send a GET request
LLHttpRequest(&request);
request.pContent = &message;
request.contentLength = strlen(message);
request.send = 0;
if(request.done) {
// Do something here
}
}
Http Server
First declare a Http Server FUB of type LLHttpServer called server. Then call the FUB cyclically. Other blocks from the library can be called using the ‘ident’ output from this block.
void _INIT ProgramInit(void)
{
server.enable = 1;
server.https = 0;
server.numClients = 4;
server.port = 1238;
server.bufferSize = 12000;
response.response.method = LLHTTP_METHOD_GET;
strcpy(response.response.uri, "/");
strcpy(response.sendBuffer, "ok");
}
void _CYCLIC ProgramCyclic(void)
{
LLHttpServer(&server);
response.enable = 1;
response.ident = server.ident;
response.pRequest = &receiveBuffer;
response.requestSize = sizeof(receiveBuffer);
response.pContent = &sendBuffer;
response.contentLength = strlen(sendBuffer);
LLHttpResponse(&response);
response.send = response.send && !response.done; // Reset after message is sent
if(response.newRequest) {
response.pContent = sendBuffer;
response.contentLength = strlen(sendBuffer);
response.send = 1;
response.status = LLHTTP_STAT_OK;
}
}
Handling unsupported requests
Its important for servers to reply to requests that they do not support. To do this we add another response FUB that listens to all URIs and DEFAULT method. Setup normal server shown in Http Server section. Then declare a response FUB of LLHttpResponse type and configure it as shown below.
void _INIT ProgramInit(void)
{
response.method = LLHTTP_METHOD_DEFAULT;
strcpy(response.uri, "**");
// Add custom 404 page
strcpy(sendBuffer, ""
"<!DOCTYPE html>"
"<html lang=\"en\">"
"<head>"
"<meta charset=\"utf-8\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />"
"<title>We've got some trouble | 404 - Resource not found</title>"
"</head>"
"<body>"
"<div class=\"cover\"><h1>Resource not found <small>404</small></h1><p class=\"lead\">The requested resource could not be found but may be available again in the future.</p></div>"
"<footer><p>Technical Contact: <a href=\"mailto:support@loupe.team\">support@loupe.team</a></p></footer>"
"</body>"
"</html>"
);
}
void _CYCLIC ProgramCyclic(void)
{
LLHttpResponse(&response);
response.send = response.send && !response.done; // Reset after message is sent
if(response.newRequest) {
response.pContent = sendBuffer;
response.contentLength = strlen(sendBuffer);
response.send = 1;
response.status = LLHTTP_STAT_NotFound;
}
}