This is a robust C library for parsing and managing Uniform Resource Identifiers (URIs). It supports the extraction of various components of a URI, such as the scheme, user info, host, port, path, query, and fragment.
- Parse and create a URI structure from a string.
- Extract different components of the URI.
- Retrieve the full URI as a string.
- Memory management for URI structures.
- A C compiler (e.g., GCC)
- CMake (version 3.18 or higher)
-
Clone the repository:
git clone https://github.com/Awl-S/uri.git cd uri
-
Compile the library and example program:
gcc -o main main.c uri.c
-
Compile the library and example program using CMake:
mkdir build cd build cmake .. make
Include the header file in your C program:
#include "uri.h"
#include "uri.h"
#include <stdio.h>
int main() {
struct Uri *my_uri = uriCreate("http://user:[email protected]:8080/path?query#fragment");
if (!my_uri) {
fprintf(stderr, "Failed to create URI\n");
return 1;
}
printf("Scheme: %s\n", uriGetScheme(my_uri));
printf("User Info: %s\n", uriGetUserInfo(my_uri));
printf("Host: %s\n", uriGetHost(my_uri));
printf("Port: %s\n", uriGetPort(my_uri));
printf("Path: %s\n", uriGetPath(my_uri));
printf("Query: %s\n", uriGetQuery(my_uri));
printf("Fragment: %s\n", uriGetFragment(my_uri));
char *full_uri = uriGetFullUri(my_uri);
if (full_uri) {
printf("Full URI: %s\n", full_uri);
free(full_uri);
}
uriDestroy(my_uri);
return 0;
}
struct Uri *uriCreate(const char *uriString);
Creates and parses a URI structure from the given string.
- uriString: URI string.
- Returns a pointer to the created Uri structure, or nullptr if it fails.
void uriDestroy(struct Uri *uri);
Destroys the URI structure and frees allocated memory.
- uri: Pointer to the Uri structure to destroy.
char *uriGetScheme(const struct Uri *uri);
Retrieves the scheme part of the URI.
- uri: Pointer to the Uri structure.
- Returns the scheme part of the URI.
char *uriGetUserInfo(const struct Uri *uri);
Retrieves the user info part of the URI.
- uri: Pointer to the Uri structure.
- Returns the user info part of the URI.
char *uriGetHost(const struct Uri *uri);
Retrieves the host part of the URI.
- uri: Pointer to the Uri structure.
- Returns the host part of the URI.
char *uriGetPort(const struct Uri *uri);
Retrieves the port part of the URI.
- uri: Pointer to the Uri structure.
- Returns the port part of the URI.
char *uriGetPath(const struct Uri *uri);
Retrieves the path part of the URI.
- uri: Pointer to the Uri structure.
- Returns the path part of the URI.
char *uriGetQuery(const struct Uri *uri);
Retrieves the query part of the URI.
- uri: Pointer to the Uri structure.
- Returns the query part of the URI.
char *uriGetFragment(const struct Uri *uri);
Retrieves the fragment part of the URI.
- uri: Pointer to the Uri structure.
- Returns the fragment part of the URI.
char *uriGetFullUri(const struct Uri *uri);
Retrieves the full URI as a string.
- uri: Pointer to the Uri structure.
- Returns the full URI string.