Ejemplo n.º 1
0
/*! \brief
 * Parse a method pointed by _next, assign its enum bit to _method, and update
 * _next past the method. Returns 1 if parse succeeded and 0 otherwise.
 */
static int parse_method_advance(str* _next, enum request_method* _method)
{
    char* end;

    if (unlikely(!_next || !_method)) {
        LOG(L_ERR, "Invalid parameter value\n");
        return 0;
    }

    if (unlikely(!_next->len || !_next->s)) {
        DBG("No input\n");
        *_method = METHOD_OTHER;
        return 1;
    }
    end=_next->s+_next->len;

    switch ((_next->s)[0]) {
    case 'A':
    case 'a':
        if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ck", 2)) {
            *_method = METHOD_ACK;
            _next->len -= 3;
            _next->s += 3;
            goto found;
        } else {
            goto unknown;
        }

    case 'B':
    case 'b':
        if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ye", 2)) {
            *_method = METHOD_BYE;
            _next->len -= 3;
            _next->s += 3;
            goto found;
        } else {
            goto unknown;
        }

    case 'C':
    case 'c':
        if ((_next->len > 5) && !strncasecmp(_next->s + 1, "ancel", 5)) {
            *_method = METHOD_CANCEL;
            _next->len -= 6;
            _next->s += 6;
            goto found;
        } else {
            goto unknown;
        }

    case 'I':
    case 'i':
        if ((_next->len > 3) &&
                ((*(_next->s + 1) == 'N') || (*(_next->s + 1) == 'n'))) {
            if (!strncasecmp(_next->s + 2, "fo", 2)) {
                *_method = METHOD_INFO;
                _next->len -= 4;
                _next->s += 4;
                goto found;
            }

            if ((_next->len > 5) && !strncasecmp(_next->s + 2, "vite", 4)) {
                *_method = METHOD_INVITE;
                _next->len -= 6;
                _next->s += 6;
                goto found;
            }
        }
        goto unknown;

    case 'M':
    case 'm':
        if ((_next->len > 6) && !strncasecmp(_next->s + 1, "essage", 6)) {
            *_method = METHOD_MESSAGE;
            _next->len -= 7;
            _next->s += 7;
            goto found;
        } else {
            goto unknown;
        }

    case 'N':
    case 'n':
        if ((_next->len > 5) && !strncasecmp(_next->s + 1, "otify", 5)) {
            *_method = METHOD_NOTIFY;
            _next->len -= 6;
            _next->s += 6;
            goto found;
        } else {
            goto unknown;
        }

    case 'O':
    case 'o':
        if ((_next->len > 6) && !strncasecmp(_next->s + 1, "ptions", 6)) {
            *_method = METHOD_OPTIONS;
            _next->len -= 7;
            _next->s += 7;
            goto found;
        } else {
            goto unknown;
        }

    case 'P':
    case 'p':
        if ((_next->len > 4) && !strncasecmp(_next->s + 1, "rack", 4)) {
            *_method = METHOD_PRACK;
            _next->len -= 5;
            _next->s += 5;
            goto found;
        }
        if ((_next->len > 6) && !strncasecmp(_next->s + 1, "ublish", 6)) {
            *_method = METHOD_PUBLISH;
            _next->len -= 7;
            _next->s += 7;
            goto found;
        }
        goto unknown;

    case 'R':
    case 'r':
        if ((_next->len > 4) &&
                ((*(_next->s + 1) == 'E') || (*(_next->s + 1) == 'e'))) {
            if (!strncasecmp(_next->s + 2, "fer", 3)) {
                *_method = METHOD_REFER;
                _next->len -= 5;
                _next->s += 5;
                goto found;
            }

            if ((_next->len > 7) && !strncasecmp(_next->s + 2, "gister", 6)) {
                *_method = METHOD_REGISTER;
                _next->len -= 8;
                _next->s += 8;
                goto found;
            }
        }
        goto unknown;

    case 'S':
    case 's':
        if ((_next->len > 8) && !strncasecmp(_next->s + 1, "ubscribe", 8)) {
            *_method = METHOD_SUBSCRIBE;
            _next->len -= 9;
            _next->s += 9;
            goto found;
        } else {
            goto unknown;
        }

    case 'U':
    case 'u':
        if ((_next->len > 5) && !strncasecmp(_next->s + 1, "pdate", 5)) {
            *_method = METHOD_UPDATE;
            _next->len -= 6;
            _next->s += 6;
            goto found;
        } else {
            goto unknown;
        }

    default:
        goto unknown;
    }

unknown:
    if (token_char(*(_next->s))) {
        do {
            _next->s++;
            _next->len--;
        } while (_next->len && token_char(*(_next->s)));
        *_method = METHOD_OTHER;
        return 1;
    } else {
        return 0;
    }
found:
    /* check if the method really ends here (if not return 0) */
    return (_next->s>=end) || (!token_char(*(_next->s)));
}
Ejemplo n.º 2
0
 /*
  * Parse a method pointed by _next, assign its enum bit to _method, and update
  * _next past the method. Returns 1 if parse succeeded and 0 otherwise.
  */
static int parse_method(str* _next, unsigned int* _method) 
 {
	 if (!_next || !_method) {
		 LOG(L_ERR, "parse_method: Invalid parameter value\n");
		 return 0;
	 }
	 
	 if (!_next->len || !_next->s) {
		 DBG("parse_method: No input\n");
		 return 1;
	 }

	 switch ((_next->s)[0]) {
	 case 'A':
	 case 'a':
		 if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ck", 2)) {
 			*_method = METH_ACK;
 			_next->len -= 3;
 			_next->s += 3;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'B':
 	case 'b':
 		if ((_next->len > 2) && !strncasecmp(_next->s + 1, "ye", 2)) {
 			*_method = METH_BYE;
 			_next->len -= 3;
 			_next->s += 3;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'C':
 	case 'c':
 		if ((_next->len > 5) && !strncasecmp(_next->s + 1, "ancel", 5)) {
 			*_method = METH_CANCEL;
 			_next->len -= 6;
 			_next->s += 6;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'I':
 	case 'i':
 		if ((_next->len > 3) &&
 		    ((*(_next->s + 1) == 'N') || (*(_next->s + 1) == 'n'))) {
 			if (!strncasecmp(_next->s + 2, "fo", 2)) {
 				*_method = METH_INFO;
 				_next->len -= 4;
 				_next->s += 4;
 				return 1;
 			}

 			if ((_next->len > 5) && !strncasecmp(_next->s + 2, "vite", 4)) {
 				*_method = METH_INVITE;
 				_next->len -= 6;
 				_next->s += 6;
 				return 1;
 			}
 		}
 		goto unknown;

 	case 'M':
 	case 'm':
 		if ((_next->len > 6) && !strncasecmp(_next->s + 1, "essage", 6)) {
 			*_method = METH_MESSAGE;
 			_next->len -= 7;
 			_next->s += 7;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'N':
 	case 'n':
 		if ((_next->len > 5) && !strncasecmp(_next->s + 1, "otify", 5)) {
 			*_method = METH_NOTIFY;
 			_next->len -= 6;
 			_next->s += 6;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'O':
 	case 'o':
 		if ((_next->len > 6) && !strncasecmp(_next->s + 1, "ptions", 6)) {
 			*_method = METH_OPTIONS;
 			_next->len -= 7;
 			_next->s += 7;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'P':
 	case 'p':
 		if ((_next->len > 4) && !strncasecmp(_next->s + 1, "rack", 4)) {
 			*_method = METH_PRACK;
 			_next->len -= 5;
 			_next->s += 5;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'R':
 	case 'r':
 		if ((_next->len > 4) &&
 		    ((*(_next->s + 1) == 'E') || (*(_next->s + 1) == 'e'))) {
 			if (!strncasecmp(_next->s + 2, "fer", 3)) {
 				*_method = METH_REFER;
 				_next->len -= 5;
 				_next->s += 5;
 				return 1;
 			}

 			if ((_next->len > 7) && !strncasecmp(_next->s + 2, "gister", 6)) {
 				*_method = METH_REGISTER;
 				_next->len -= 8;
 				_next->s += 8;
 				return 1;
 			}
 		}
 		goto unknown;

 	case 'S':
 	case 's':
 		if ((_next->len > 8) && !strncasecmp(_next->s + 1, "ubscribe", 8)) {
 			*_method = METH_SUBSCRIBE;
 			_next->len -= 9;
 			_next->s += 9;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	case 'U':
 	case 'u':
 		if ((_next->len > 5) && !strncasecmp(_next->s + 1, "pdate", 5)) {
 			*_method = METH_UPDATE;
 			_next->len -= 6;
 			_next->s += 6;
 			return 1;
 		} else {
 			goto unknown;
 		}

 	default:
 		goto unknown;
 	}
 
 unknown:
 	if (token_char(*(_next->s))) {
 		do { 
 			_next->s++;
 			_next->len--;
 		} while (_next->len && token_char(*(_next->s)));
 		*_method = METH_UNKNOWN;
 		return 1;
 	} else {
 		return 0;
 	}
 }