libwinyl
Loading...
Searching...
No Matches
yuarel_integration.c

Demonstrates integration with yuarel.
Link with: -lyuarel

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winyl/winyl.h>
#include <winyl/request.h>
#include <winyl/header.h>
#include <yuarel.h>
int main(int argc, char* argv[]) {
// grab the last argument to the program into the variable,
// or prefill it. We do this to ensure we can modify the string,
// because of yuarel limitations.
char* url = malloc(argc <= 1 ? 28 : strlen(argv[argc - 1] + 1));
strcpy(url, argc <= 1 ? "http://postman-echo.com/get" : argv[argc - 1]);
printf("Requesting %s\n", url);
// parse the URL with yuarel
struct yuarel url_info;
if (yuarel_parse(&url_info, url) == -1) {
printf("Failed parsing URL\n");
exit(1);
}
// check if port is set, otherwise set it to 80
// 0 is a yuarel default
winyl host = winyl_open(url_info.host, url_info.port == 0 ? 80 : url_info.port);
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);
}
printf("\n");
// WINYL_REQUEST_SLASH adds a slash to the beginning of the path.
// We need this because yuarel removes the first slash from the path.
winyl_response res = winyl_request(&host, url_info.path, WINYL_REQUEST_SLASH);
// print out response body
printf("%s\n", res.body);
winyl_close(&host);
free(url);
exit(0);
}
Contains functions related to winyl_header.
Contains the winyl_request function and related.
void winyl_response_close(winyl_response *response)
Frees memory used up by response.
#define WINYL_REQUEST_SLASH
Adds a slash to the beginning of the path passed. Useful for yuarel.
Definition request.h:46
winyl_response winyl_request(winyl *host, char *path, int flags)
Performs a HTTP request.
Represents a HTTP response.
Definition request.h:13
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.
winyl winyl_open(char *hostname, int port)
Opens an HTTP host with the specified port.