Vou falar de outra forma interessante de manipular a resposta HTTP com OrionSocket, através da estrutura orion_httpResponse e a função orion_httpReqRes().
No post anterior eu mostrei como receber a resposta numa string do tipo C. Mas é possivel também ter a resposta já numa estrutura.
A estrutura orion_httpResponse tem a seguinte declaração:
header <orion/socket/http.h>
typedef struct
{
_uint8 version; /* HTTP version. 1.0 || 1.1 */
_uint16 code; /* Status Code */
char* message; /* Server Message */
char* serverName; /* Server Name */
char* date; /* Date */
char* expires; /* Expires time */
char* location; /* Location. */
char* mime_version; /* MIME-VERSION */
char* content_type; /* Content-Type */
char* charset; /* Charset */
_uint32 content_length; /* Length of the content; */
nameValue *header; /* HTTP Headers */
_uint8 headerLen; /* Number of headers */
nameValue *cookie; /* Set Cookie */
_uint8 cookieLen; /* Number of cookies */
char* body; /* Body of the response */
} orion_httpResponse;
A função que faz a requisição e retorna as informações nessa estrutura é a orion_httpReqRes(), o protótipo dela é o seguinte:
_uint8 orion_httpReqRes(orion_httpRequest* req, orion_httpResponse** res2);
O primeiro parametro é a estrutura com as informações da requisição e o segundo o ponteiro para o ponteiro da estrutura resposta.
Abaixo um exemplo simples com a forma de utilização:
#include <stdio.h>
#include <stdlib.h>
#include <orion/socket/http.h>
int main(int argc, char** argv)
{
orion_httpRequest* req = NULL;
orion_httpResponse* res = NULL;
int i;
// Inicializa a requisição
orion_initHttpRequest(&req);
orion_setHttpRequestHost(req, "www.unicheck.com.br", 80);
// Não há a necessidade de inicializar res (orion_httpResponse)
int code = orion_httpReqRes(req, &res);
// or if (!code)
if (code == ORIONSOCKET_OK) // ORIONSOCKET_OK = 0x00
{
/**
* A resposta encontra-se na estrutura orion_httpResponse *res
*/
printf("################################\n");
printf("# HEADERS:\n");
printf("# Http Version: 1.%d\n", res->version);
printf("# Http Status Code: %d\n", res->code);
printf("# Server Message: %s\n", res->message);
printf("# Server Name: %s\n", res->serverName);
printf("# Server Date: %s\n", res->date);
printf("# Todos os Headers: \n");
for (i = 0; i < res->headerLen; i++)
{
printf("# %s: %s\n", res->header[i].name, res->header[i].value);
}
printf("################################\n");
printf("# Body Content: \n");
printf("%s\n", res->body);
}
// Desaloca a memória da requisição e da resposta
orion_cleanupHttpRequest(req);
orion_cleanupHttpResponse(res);
return 0;
}
Tenho muita coisa pra fazer ainda, mas já dá pra brincar