t_bool ft_hmatch(char const *str, char const *pattern) { while (*str != '\0') if (hmatch(str, pattern)) return (true); else str++; return (false); }
static void compute_info(char *l, t_http_head *h) { if (hmatch(l, GET_) || hmatch(l, POST_) || hmatch(l, PUT_) || hmatch(l, DELETE_)) parse_request(l, h); else if (hmatch(l, HOST_)) h->host = DDS_HSUB(l, HOST_); else if (hmatch(l, CONN_)) h->connexion = DDS_HSUB(l, CONN_); else if (hmatch(l, ACC_)) h->accept = DDS_HSUB(l, ACC_); else if (hmatch(l, US_AG_)) h->user_agent = DDS_HSUB(l, US_AG_); else if (hmatch(l, ACC_ENC_)) h->accept_enc = DDS_HSUB(l, ACC_ENC_); else if (hmatch(l, ACC_LANG_)) h->accept_lang = DDS_HSUB(l, ACC_LANG_); }
/* ** ft_hmatch ** ---- ** String matching ** ---- ** Special chars: ** '?' Match any char except white-space ** '*' Match 0 or more ? ** ' ' Match 1 or more white-space then any char ** ---- ** Examples: (all match) ** "abcdef" "bcd" ** "abcdef" "*c*" ** "abcdef" "*c?e*" ** "abcdef" "*c?e*" ** " a bcd e " "a bcd e" ** "abc defghij" "bc f?h" ** ---- ** Return true if str match pattern ** false otherwise */ static t_bool hmatch(char const *str, char const *pattern) { if (*pattern == '\0') return (true); if (*pattern == '*') return (hmatch(str, pattern + 1) || (*str != '\0' && hmatch(str + 1, pattern))); if (!(*pattern == '?' && *str != '\0') && *pattern != *str) return (false); if (*pattern == ' ') { while (*(++pattern) == ' ') ; while (*(++str) != '\0') if (hmatch(str, pattern)) return (true); return (false); } return (hmatch(str + 1, pattern + 1)); }
static void parse_request(char *r, t_http_head *h) { char **spli; spli = ft_strsplit(r, ' '); if (spli) { if (hmatch(r, GET_)) h->type = GET; else if (hmatch(r, POST_)) h->type = POST; else if (hmatch(r, PUT_)) h->type = PUT; else if (hmatch(r, DELETE_)) h->type = DELETE; else h->type = UNKNOWN; if (spli[1]) h->target = ft_strjoin(".", spli[1]); if (spli[2]) h->protocol = spli[2]; } }