<commit_before>const mime = require("./mime-types"),
	fs = require("fs"),
	os = require("os"),
	path = require("path"),
	url = require("url");

class FormData {
	constructor(server) {
		let cfg = server.getCfg("file_uploads");

		this.server = server;

		this.buffer = new Buffer("\r\n");

		this.type = "";

		this.boundary = "";

		this.bytesReceived = 0;

		this.maxBytes = cfg.status === "on" &&
			cfg.limit ? cfg.limit : 1024 * 1024;

		this.uploaded = false;

		this.headers(() => {
			server.req.on("data", data => {
				server.form.write(data);
			}).on("end", () => {
				server.form.end();
			});
		});
	}

	headers(cb) {
		let header = this.server.req.headers["content-type"],
			match;

		if (header && header.match(/urlencoded/i)) {
			this.type = "urlencoded";
		}

		if (header && header.match(/multipart/i)) {
			this.type = "multipart";

			match = header.match(/boundary=(?:"([^"]+)"|([^;]+))/i);

			if (match) {
				match = match[1] || match[2];

				this.boundary = new Buffer(match.length + 4);
				this.boundary.write("\r\n--", 0);
				this.boundary.write(match, 4);
			} else {
				this.server.sendErrorPage(500);

				return void 0;
			}
		}

		cb();
	}

	write(data) {
		this.buffer = Buffer.concat([this.buffer, data]);

		this.bytesReceived += data.length;

		if (this.bytesReceived > this.maxBytes) {
			this.server.sendErrorPage(413);
		}
	}

	end() {
		if (this.server.res.finished) {
			return void 0;
		}

		if (this.type === "urlencoded") {
			this.urlencoded();
		}

		if (this.type === "multipart") {
			this.multipart();
		}
	}

	urlencoded() {
		let buffer = this.buffer;

		buffer = buffer.toString();
		buffer = buffer.replace(/\r\n/g, "");

		this.server.POST = url.parse("/?" + buffer, true).query;

		this.server.sendFile();
	}

	multipart() {
		let cfg = this.server.getCfg("file_uploads"),
			part = {},
			boundary = this.boundary,<commit_msg>Improve file uploading<commit_after>const mime = require("./mime-types"),
	fs = require("fs"),
	os = require("os"),
	path = require("path"),
	url = require("url");

class FormData {
	constructor(server) {
		let cfg = server.getCfg("file_uploads");

		this.server = server;

		this.buffer = new Buffer("\r\n");

		this.type = "";

		this.boundary = "";

		this.bytesReceived = 0;

		this.maxBytes = cfg.limit || 1048576;

		if (cfg.status !== "on") {
			this.maxBytes = 0;
		}

		this.uploaded = false;

		this.headers(() => {
			server.req.on("data", data => {
				this.write(data);
			}).on("end", () => {
				this.end();
			});
		});
	}

	headers(cb) {
		let header = this.server.req.headers["content-type"],
			match;

		if (header && header.match(/urlencoded/i)) {
			this.type = "urlencoded";
		}

		if (header && header.match(/multipart/i)) {
			this.type = "multipart";

			match = header.match(/boundary=(?:"([^"]+)"|([^;]+))/i);

			if (match) {
				match = match[1] || match[2];

				this.boundary = new Buffer(match.length + 4);
				this.boundary.write("\r\n--", 0);
				this.boundary.write(match, 4);
			} else {
				this.server.sendErrorPage(500);

				return void 0;
			}
		}

		cb();
	}

	write(data) {
		this.bytesReceived += data.length;

		if (this.bytesReceived > this.maxBytes) {
			this.server.sendErrorPage(413);

			return void 0;
		}

		this.buffer = Buffer.concat([this.buffer, data]);
	}

	end() {
		if (this.server.res.finished) {
			return void 0;
		}

		if (this.type === "urlencoded") {
			this.urlencoded();
		}

		if (this.type === "multipart") {
			this.multipart();
		}
	}

	urlencoded() {
		let buffer = this.buffer;

		buffer = buffer.toString();
		buffer = buffer.replace(/\r\n/g, "");

		this.server.POST = url.parse("/?" + buffer, true).query;

		this.server.sendFile();
	}

	multipart() {
		let cfg = this.server.getCfg("file_uploads"),
			part = {},
			boundary = this.boundary,
{'fraction_non_alphanumeric': 0.20635337358886846, 'fraction_numerical': 0.01181412444211079, 'mean_word_length': 2.9440993788819876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '716498', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1363, 'n_words': 247}