Beispiel #1
0
/**
 * Parses HTTP headers. Only a few headers are actually processed, while most
 * values are discarded.
 */
int parse_headers(struct parser *p) {
    int r;

    while (TRUE) {
        switch (p->state) {
        case PARSING_HEADERS:
            r = parse_constant(p, CRLF, 2);
            if (r != PARSING_ERROR)
                return r;
            p->state = PARSING_HEADER_NAME;
            debug("parsing header");

        case PARSING_HEADER_NAME:
        case PARSING_HEADER_NAME_ANY:
            r = parse_header_name(p);
            if (r != PARSING_DONE)
                return r;
            debug("parsed header name");
        }

        switch (p->state) {
        case PARSING_HEADER_VALUE:
            r = parse_header_value(p);
            break;

        case PARSING_HEADER_CONTENT_LENGTH:
            r = parse_header_content_length(p);
            break;

        default:
            return PARSING_ERROR;
        }

        if (r != PARSING_DONE)
            return r;
        p->state = PARSING_HEADERS;
        debug("parsed header value");
    }
    return PARSING_DONE;
}
Beispiel #2
0
static options_t *parse_options(int argc, char *argv[])
{
	options_t *options = malloc_s(sizeof(options_t));
	memset(options, 0, sizeof(options_t));

	// parameters for getopt_long() function
	static const char short_options[] = "f:a:c:h:s:V";

	static const struct option long_options[] = {
		{ "help",          no_argument,         NULL,  1  },
		{ "format",        required_argument,   NULL, 'f' },
		{ "all",           no_argument,         NULL, 'a' },
		{ "content",       no_argument,         NULL, 'c' },
		{ "header",        required_argument,   NULL, 'h' },
		{ "section-name",  required_argument,   NULL, 's' },
		{ "section-index", required_argument,   NULL,  2  },
		{ "version",       no_argument,         NULL, 'V' },
		{  NULL,           0,                   NULL,  0  }
	};

	// Setting the default option
	options->content = true;

	int c, ind;
	while ((c = getopt_long(argc, argv, short_options, long_options, &ind)))
	{
		if (c < 0)
			break;

		switch (c)
		{
			case 1:     // --help option
				usage();
				exit(EXIT_SUCCESS);
			case 'f':
				if (output_set_format_by_name(optarg) < 0)
					EXIT_ERROR("invalid format option");
				break;
			case 'a':
				options->all = true;
				break;
			case 'c': // default
				options->all = false; //TODO remover?
				options->content = true;
				break;
			case 's':
				options->all = false;
				options->headers.all = false;
				// TODO: How do we need to handle non-ascii names?
				options->sections.name = strdup(optarg);
				break;
			case 2:
				options->all = false;
				options->headers.all = false;
				options->sections.index = strtol(optarg, NULL, 10);
				if (options->sections.index < 1 || options->sections.index > MAX_SECTIONS) {
					EXIT_ERROR("Bad argument for section-index,");
				}
				break;
			case 'V':
				printf("%s %s\n%s\n", PROGRAM, TOOLKIT, COPY);
				exit(EXIT_SUCCESS);
			case 'h':
				options->all = false;
				options->headers.all = false;
				parse_header_name(options, optarg);
				break;
			default:
				fprintf(stderr, "%s: try '--help' for more information\n", PROGRAM);
				exit(EXIT_FAILURE);
		}
	}

	// TODO: Warn about simultaneous usage of -h, -s, and --section-index.

	return options;
}