libwinyl
Loading...
Searching...
No Matches
basic.c

A simple example on how to utilize the library.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winyl/winyl.h>
#include <winyl/request.h>
#include <winyl/header.h>
int main(int argc, char* argv[]) {
winyl host = winyl_open("postman-echo.com", 80);
if (host.error != 0) {
// some of these cannot happen when things are hardcoded,
// but they're good to show off all the errors
if (host.error == WINYL_ERROR_PORT) printf("Invalid port (outside of 0-65535)\n");
else if (host.error == WINYL_ERROR_DNS) printf("Error calling net_gethostbyname()\n");
else if (host.error == WINYL_ERROR_MALLOC) printf("Failed to allocate memory\n");
winyl_close(&host);
exit(1);
}
winyl_response res = winyl_request(&host, "/get", 0);
if (res.error != 0) {
if (host.error == WINYL_ERROR_SOCKET) printf("Error calling net_socket()\n");
else if (host.error == WINYL_ERROR_CONNECT) printf("Error calling net_connect()\n");
else if (host.error == WINYL_ERROR_SEND) printf("Error calling net_send()\n");
else if (host.error == WINYL_ERROR_RECV) printf("Error calling net_recv()\n");
else if (host.error == WINYL_ERROR_CLOSE) printf("Error calling net_close()\n");
else if (host.error == WINYL_ERROR_MALLOC) printf("Failed to allocate memory\n");
winyl_close(&host);
exit(1);
}
printf("HTTP/%d.%d %d %s\n", res.http_version / 10, res.http_version % 10, res.status, res.status_text);
// this simply prints all headers,
// you do not have to process this, check out headers.c
for (int i = 0; i < res.headers_count; i++) {
printf("%s: %s\n", res.headers[i].name, res.headers[i].value);
}
printf("%s\n", res.body);
// close all used resources like a good C programmer
winyl_close(&host);
exit(0);
}
Contains functions related to winyl_header.
Contains the winyl_request function and related.
#define WINYL_ERROR_CLOSE
Error calling net_close().
Definition request.h:57
#define WINYL_ERROR_SEND
Error calling net_send().
Definition request.h:53
#define WINYL_ERROR_RECV
Error calling net_recv().
Definition request.h:55
#define WINYL_ERROR_CONNECT
Error calling net_connect().
Definition request.h:51
void winyl_response_close(winyl_response *response)
Frees memory used up by response.
#define WINYL_ERROR_SOCKET
Error calling net_socket().
Definition request.h:49
winyl_response winyl_request(winyl *host, char *path, int flags)
Performs a HTTP request.
char * name
header name
Definition header.h:17
char * value
header value
Definition header.h:19
Represents a HTTP response.
Definition request.h:13
int http_version
HTTP version. See WINYL_HTTP_1_0 and similar.
Definition request.h:20
struct winyl_header * headers
Header collection; see header.h.
Definition request.h:27
int error
Contains the error code after any winyl call.
Definition request.h:15
char * body
Response body.
Definition request.h:34
int status
HTTP status.
Definition request.h:22
char * status_text
HTTP status text.
Definition request.h:24
int headers_count
Header count.
Definition request.h:29
Represents a HTTP host.
Definition winyl.h:27
int error
Contains the error code after any winyl call.
Definition winyl.h:29
Contains the winyl_open function and related.
#define WINYL_ERROR_DNS
Call to net_gethostbyname() failed.
Definition winyl.h:60
void winyl_close(winyl *host)
Frees memory used up by host.
#define WINYL_ERROR_MALLOC
Allocating memory failed.
Definition winyl.h:64
winyl winyl_open(char *hostname, int port)
Opens an HTTP host with the specified port.
#define WINYL_ERROR_PORT
Speficied port is not valid.
Definition winyl.h:58