/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2021 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "libc/str/str.h"
#include "net/http/http.h"

/**
 * Parses X-Forwarded-For.
 *
 * This header is used by reverse proxies. For example:
 *
 *     X-Forwarded-For: 203.0.110.2, 203.0.113.42:31337
 *
 * The port is optional and will be set to zero if absent.
 *
 * @param s is input data
 * @param n if -1 implies strlen
 * @param ip receives last/right ip on success if not NULL
 * @param port receives port on success if not NULL
 * @return 0 on success or -1 on failure
 * @see RFC7239's poorly designed Forwarded header
 */
int ParseForwarded(const char *s, size_t n, uint32_t *ip, uint16_t *port) {
  int c, t;
  size_t i;
  char *r;
  uint32_t x;
  if (n == -1) n = s ? strlen(s) : 0;
  if (n) {
    t = x = i = 0;
    if ((r = memrchr(s, ',', n))) {
      i = r - s;
      if ((s[++i] & 255) == ' ') ++i;  // skip optional space
    }
    do {
      c = s[i++] & 255;
      if (isdigit(c)) {
        t *= 10;
        t += c - '0';
        if (t > 255) {
          return -1;
        }
      } else if (c == '.') {
        x <<= 8;
        x |= t;
        t = 0;
      } else if (c == ':') {
        break;
      } else {
        return -1;
      }
    } while (i < n);
    x <<= 8;
    x |= t;
    t = 0;
    if (c == ':') {
      while (i < n) {
        c = s[i++] & 255;
        if (isdigit(c)) {
          t *= 10;
          t += c - '0';
          if (t > 65535) {
            return -1;
          }
        } else {
          return -1;
        }
      }
    }
    if (ip) *ip = x;
    if (port) *port = t;
    return 0;
  } else {
    return -1;
  }
}
