<commit_before>/*
 * Copyright (C) 2011 Linkeos.
 *
 * This file is part of BloatIt.
 *
 * BloatIt is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * BloatIt is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with BloatIt. If not, see <http://www.gnu.org/licenses/>.
 */
package com.bloatit.framework.xcgiserver;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import com.bloatit.common.Log;
import com.bloatit.framework.exceptions.highlevel.MeanUserException;
import com.bloatit.framework.utils.parameters.Parameters;
import com.bloatit.framework.xcgiserver.postparsing.PostParameter;
import com.bloatit.framework.xcgiserver.postparsing.PostParser;
import com.bloatit.framework.xcgiserver.postparsing.exceptions.MalformedPostException;

/**
 * A class to describe elements transmitted by an http POST query
 */
public class HttpPost {
    private final static String UPLOAD_TEMP_DIRECTORY = System.getProperty("user.home") + "/.local/share/bloatit/uploads_temp/";
    private final Parameters parameters = new Parameters();

    /**
     * <p>
     * Construct an HttpPost from a POST request
     * </p>
     *
     * @param is The stream to read containing the post Data
     * @param length The length of the post content
     * @param contentType The contentType of the post (obtained previouysly from
     *            the request)
     * @throws IOException
     */
    public HttpPost(final InputStream is, final int length, final String contentType) throws IOException {
        readBytes(is, length, contentType);
    }

    /**
     * Gets the list of parameters
     *
     * @return the list of post parameters for the page
     */
    public final Parameters getParameters() {
        return parameters;
    }

    /**
     * <p>
     * Parses the post and fills the list of parameters
     * </p>
     *
     * @param postStream the stream to read post from
     * @param length the length of the post
     * @param contentType the contentType of the post (text/plain,
     *            multipart/form-data ...)
     * @throws IOException
     */
    private void readBytes(final InputStream postStream, final int length, final String contentType) throws IOException {
        final PostParser parser = new PostParser(postStream, length, contentType, UPLOAD_TEMP_DIRECTORY);
        PostParameter pp;
        while ((pp = getNext(parser)) != null) {
            parameters.add(pp.getName(), pp.getValue());
        }
    }

    /**
     * Gets the next PostParameter in the parser, ignoring the exceptions
     *
     * @param parser the <code>POST</code> parser from which content is read
     * @return the next <code>PostParameter</code> or <code>null</code> if no
     *         more content is available
     */
    private PostParameter getNext(final PostParser parser) {
        while (true) {
            try {
                final PostParameter pp = parser.readNext();<commit_msg>Fix post parsing: always ignore empty parameters<commit_after>/*
 * Copyright (C) 2011 Linkeos.
 *
 * This file is part of BloatIt.
 *
 * BloatIt is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * BloatIt is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with BloatIt. If not, see <http://www.gnu.org/licenses/>.
 */
package com.bloatit.framework.xcgiserver;

import static com.bloatit.framework.utils.StringUtils.isEmpty;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

import com.bloatit.common.Log;
import com.bloatit.framework.exceptions.highlevel.MeanUserException;
import com.bloatit.framework.utils.parameters.Parameters;
import com.bloatit.framework.xcgiserver.postparsing.PostParameter;
import com.bloatit.framework.xcgiserver.postparsing.PostParser;
import com.bloatit.framework.xcgiserver.postparsing.exceptions.MalformedPostException;

/**
 * A class to describe elements transmitted by an http POST query
 */
public class HttpPost {
    private final static String UPLOAD_TEMP_DIRECTORY = System.getProperty("user.home") + "/.local/share/bloatit/uploads_temp/";
    private final Parameters parameters = new Parameters();

    /**
     * <p>
     * Construct an HttpPost from a POST request
     * </p>
     * 
     * @param is The stream to read containing the post Data
     * @param length The length of the post content
     * @param contentType The contentType of the post (obtained previouysly from
     *            the request)
     * @throws IOException
     */
    public HttpPost(final InputStream is, final int length, final String contentType) throws IOException {
        readBytes(is, length, contentType);
    }

    /**
     * Gets the list of parameters
     * 
     * @return the list of post parameters for the page
     */
    public final Parameters getParameters() {
        return parameters;
    }

    /**
     * <p>
     * Parses the post and fills the list of parameters
     * </p>
     * 
     * @param postStream the stream to read post from
     * @param length the length of the post
     * @param contentType the contentType of the post (text/plain,
     *            multipart/form-data ...)
     * @throws IOException
     */
    private void readBytes(final InputStream postStream, final int length, final String contentType) throws IOException {
        final PostParser parser = new PostParser(postStream, length, contentType, UPLOAD_TEMP_DIRECTORY);
        PostParameter pp;
        while ((pp = getNext(parser)) != null) {
            if (!isEmpty(pp.getName()) && !isEmpty(pp.getValue())) {
                parameters.add(pp.getName(), pp.getValue());
            }
        }
    }

    /**
     * Gets the next PostParameter in the parser, ignoring the exceptions
     * 
     * @param parser the <code>POST</code> parser from which content is read
     * @return the next <code>PostParameter</code> or <code>null</code> if no
     *         more content is available
     */
    private PostParameter getNext(final PostParser parser) {
        while (true) {
            try {
                final PostParameter pp = parser.readNext();
{'fraction_non_alphanumeric': 0.08447293447293447, 'fraction_numerical': 0.0014245014245014246, 'mean_word_length': 2.966666666666667, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4555233', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1870, 'n_words': 697}