libwinyl
Loading...
Searching...
No Matches
headers.c

Demonstrates functions from header.h

#include <stdio.h>
#include <stdlib.h>
#include <limits.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) {
// We don't handle errors properly here,
// please see basic.c for proper error handling.
printf("Error code %d\n", host.error);
winyl_close(&host);
exit(1);
}
// add a header
winyl_add_header(&host, "Accept-Language", "*");
// check its value
printf("Accept-Language value: %s\n", winyl_get_header_value(&host, "Accept-Language"));
// overwrite its value
winyl_add_header(&host, "Accept-Language", "en");
// check its value... again
printf("Updated Accept-Language value: %s\n", winyl_get_header_value(&host, "Accept-Language"));
// remove it from the requst
winyl_remove_header(&host, "Accept-Language");
// check its presence (for demonstration purposes only,
// you don't have to do this after winyl_remove_header call)
printf("Is it present? %s\n", winyl_has_header(&host, "Accept-Language") == -1 ? "No" : "Yes");
// add a different header, with a "dynamic value",
// to show that libwinyl clones the input string and that it can be safely freed
// create a string from INT_MAX
// WINYL_INTLEN is used here only for convenience,
// please note that it only handles positive integers
char* value = malloc(WINYL_INTLEN(INT_MAX) + 1);
sprintf(value, "%d", INT_MAX);
// made up header name
winyl_add_header(&host, "X-INT-MAX", value);
free(value);
winyl_response res = winyl_request(&host, "/get", 0);
if (res.error != 0) {
printf("Error code %d\n", res.error);
winyl_close(&host);
exit(1);
}
// check if header is present
printf("Is Content-Type present? %s\n", winyl_has_header2(&res, "Content-Type") == -1 ? "No" : "Yes");
// check and print header value
if (winyl_has_header2(&res, "Content-Type") != -1) {
printf("Value of Content-Type: %s\n", winyl_get_header_value2(&res, "Content-Type"));
}
printf("Body:\n%s\n", res.body);
winyl_close(&host);
exit(0);
}
Contains functions related to winyl_header.
int winyl_has_header2(winyl_response *response, char *name)
Variant of winyl_has_header that works with responses.
int winyl_has_header(winyl *host, char *name)
Returns the index of the header with name.
int winyl_add_header(winyl *host, char *name, char *value)
Adds a header to the request. Returns the index of it.
char * winyl_get_header_value2(winyl_response *response, char *name)
Variant of winyl_get_header_value that works with responses.
int winyl_remove_header(winyl *host, char *name)
Removes a header from the request with name.
char * winyl_get_header_value(winyl *host, char *name)
Gets the value of header with name.
Contains the winyl_request function and related.
void winyl_response_close(winyl_response *response)
Frees memory used up by response.
winyl_response winyl_request(winyl *host, char *path, int flags)
Performs a HTTP request.
Represents a HTTP response.
Definition request.h:13
int error
Contains the error code after any winyl call.
Definition request.h:15
char * body
Response body.
Definition request.h:34
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.
void winyl_close(winyl *host)
Frees memory used up by host.
#define WINYL_INTLEN(x)
Calculates the amount of digits in a positive integer.
Definition winyl.h:15
winyl winyl_open(char *hostname, int port)
Opens an HTTP host with the specified port.