// Written by retoor@molodetz.nl

// This code defines a URL parser in C that extracts components such as the
// scheme, hostname, port, path, and query from a given URL.

// Includes:
// - <stdio.h> for standard I/O operations
// - <string.h> for string manipulation functions
// - <stdlib.h> for memory allocation and management

// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS",
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
// THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef R_URL_H
#define R_URL_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  char scheme[16];
  char hostname[256];
  char port[8];
  char path[512];
  char query[512];
} url_t;

int parse_url(const char *url, url_t *parsed_url) {
  memset(parsed_url, 0, sizeof(url_t));

  const char *scheme_end = strstr(url, "://");
  if (!scheme_end) {
    return -1;
  }

  strncpy(parsed_url->scheme, url, scheme_end - url);
  parsed_url->scheme[scheme_end - url] = '\0';

  const char *hostname_start = scheme_end + 3;
  const char *port_start = strchr(hostname_start, ':');
  const char *path_start = strchr(hostname_start, '/');
  const char *query_start = strchr(hostname_start, '?');

  if (port_start && (!path_start || port_start < path_start)) {
    size_t hostname_length = port_start - hostname_start;
    if (hostname_length >= sizeof(parsed_url->hostname)) {
      fprintf(stderr, "Hostname is too long\n");
      return -1;
    }
    strncpy(parsed_url->hostname, hostname_start, hostname_length);
    parsed_url->hostname[hostname_length] = '\0';

    size_t port_length = query_start ? (size_t)(query_start - port_start - 1)
                                     : strlen(port_start + 1);
    if (port_length >= sizeof(parsed_url->port)) {
      fprintf(stderr, "Port value is too long\n");
      return -1;
    }
    strncpy(parsed_url->port, port_start + 1, port_length);
    parsed_url->port[port_length] = '\0';
  } else {
    size_t hostname_length =
        path_start ? (size_t)(path_start - hostname_start)
                   : (query_start ? (size_t)(query_start - hostname_start)
                                  : strlen(hostname_start));
    if (hostname_length >= sizeof(parsed_url->hostname)) {
      fprintf(stderr, "Hostname is too long\n");
      return -1;
    }
    strncpy(parsed_url->hostname, hostname_start, hostname_length);
    parsed_url->hostname[hostname_length] = '\0';
  }

  if (path_start) {
    if (query_start) {
      strncpy(parsed_url->path, path_start, query_start - path_start);
      parsed_url->path[query_start - path_start] = '\0';
    } else {
      strcpy(parsed_url->path, path_start);
    }
  }

  if (query_start) {
    strcpy(parsed_url->query, query_start + 1);
  }

  return 0;
}
#endif