<commit_before>/*
 * JGrapes Event Driven Framework
 * Copyright (C) 2016  <NAME>
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program 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 General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */

package org.jgrapes.http.demo.httpserver;

import java.awt.event.InputEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import org.jdrupes.httpcodec.Decoder;
import org.jdrupes.httpcodec.ProtocolException;
import org.jdrupes.httpcodec.protocols.http.HttpField;
import org.jdrupes.httpcodec.protocols.http.HttpResponse;
import org.jdrupes.httpcodec.protocols.http.HttpConstants.HttpStatus;
import org.jdrupes.httpcodec.types.MediaType;
import org.jdrupes.httpcodec.util.FormUrlDecoder;
import org.jgrapes.core.Channel;
import org.jgrapes.core.Component;
import org.jgrapes.core.Components;
import org.jgrapes.core.NamedChannel;
import org.jgrapes.core.annotation.Handler;
import org.jgrapes.http.HttpServer;
import org.jgrapes.http.StaticContentDispatcher;
import org.jgrapes.http.annotation.RequestHandler;
import org.jgrapes.http.events.EndOfRequest;
import org.jgrapes.http.events.GetRequest;
import org.jgrapes.http.events.PostRequest;
import org.jgrapes.io.FileStorage;
import org.jgrapes.io.IOSubchannel;
import org.jgrapes.io.NioDispatcher;
import org.jgrapes.io.events.Input;
import org.jgrapes.io.util.ManagedBuffer;
import org.jgrapes.io.util.ManagedByteBuffer;
import org.jgrapes.io.util.PermitsPool;
import org.jgrapes.net.SslServer;
import org.jgrapes.net.TcpServer;

/**
 *
 */
public class HttpServerDemo extends Component {

	private class FormContext {
		public FormUrlDecoder fieldDecoder = new FormUrlDecoder();
	}
	
	private Map<IOSubchannel, FormContext> formContexts 
		= Collections.synchronizedMap(new WeakHashMap<>());
	
	@RequestHandler(patterns="/form")
	public void onPost(PostRequest request) {
		HttpResponse response = request.request().response().get();
		for (IOSubchannel channel: request.channels(IOSubchannel.class)) {
			formContexts.put(channel, new FormContext());
		}		
	}
	
	@Handler
	public void onInput(Input<ManagedByteBuffer> event) {
		for (IOSubchannel channel: event.channels(IOSubchannel.class)) {
			FormContext ctx = formContexts.get(channel);
			ctx.fieldDecoder.addData(event.buffer().backingBuffer());
		}
//		while (true) {
//		out.clear();
//		Decoder.Result<?> decoderResult = null;
//		try {
//			decoderResult = engine.decode(in, out, false);
//		} catch (ProtocolException e) {
//			return;
//		}
//		out.flip();
//		fieldDecoder.addData(out);
//		if (decoderResult.isOverflow()) {
//			continue;
//		}
//		if (decoderResult.isUnderflow()) {
//			in.clear();
//			channel.read(in);
//			in.flip();
//			continue;
//		}
//		break;
//	}
//	response.setStatus(HttpStatus.OK);
//	response.setMessageHasBody(true);
//	response.setField(HttpField.CONTENT_TYPE,
//			MediaType.builder().setType("text", "plain")
//			.setParameter("charset", "utf-8").build());
//	String data = "First name: " + fieldDecoder.fields().get("firstname")
//	        + "\r\n" + "Last name: "
//	        + fieldDecoder.fields().get("lastname");
//	ByteBuffer body = ByteBuffer.wrap(data.getBytes("utf-8"));
//	sendResponse(response, body, true);<commit_msg>Testing per channel handling.<commit_after>/*
 * JGrapes Event Driven Framework
 * Copyright (C) 2016  <NAME>
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program 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 General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */

package org.jgrapes.http.demo.httpserver;

import java.awt.event.InputEvent;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import org.jdrupes.httpcodec.Decoder;
import org.jdrupes.httpcodec.ProtocolException;
import org.jdrupes.httpcodec.protocols.http.HttpField;
import org.jdrupes.httpcodec.protocols.http.HttpResponse;
import org.jdrupes.httpcodec.protocols.http.HttpConstants.HttpStatus;
import org.jdrupes.httpcodec.types.MediaType;
import org.jdrupes.httpcodec.util.FormUrlDecoder;
import org.jgrapes.core.Channel;
import org.jgrapes.core.Component;
import org.jgrapes.core.Components;
import org.jgrapes.core.NamedChannel;
import org.jgrapes.core.annotation.Handler;
import org.jgrapes.http.HttpServer;
import org.jgrapes.http.StaticContentDispatcher;
import org.jgrapes.http.annotation.RequestHandler;
import org.jgrapes.http.events.EndOfRequest;
import org.jgrapes.http.events.GetRequest;
import org.jgrapes.http.events.PostRequest;
import org.jgrapes.io.FileStorage;
import org.jgrapes.io.IOSubchannel;
import org.jgrapes.io.NioDispatcher;
import org.jgrapes.io.events.Input;
import org.jgrapes.io.util.ManagedBuffer;
import org.jgrapes.io.util.ManagedByteBuffer;
import org.jgrapes.io.util.PermitsPool;
import org.jgrapes.net.SslServer;
import org.jgrapes.net.TcpServer;

/**
 *
 */
public class HttpServerDemo extends Component {

	private class FormContext {
		public FormUrlDecoder fieldDecoder = new FormUrlDecoder();
	}
	
	private Map<IOSubchannel, FormContext> formContexts 
		= Collections.synchronizedMap(new WeakHashMap<>());
	
	@RequestHandler(patterns="/form")
	public void onPost(PostRequest request, IOSubchannel channel) {
		HttpResponse response = request.request().response().get();
		formContexts.put(channel, new FormContext());
	}
	
	@Handler
	public void onInput(Input<ManagedByteBuffer> event, IOSubchannel channel) {
		FormContext ctx = formContexts.get(channel);
		ctx.fieldDecoder.addData(event.buffer().backingBuffer());

//		while (true) {
//		out.clear();
//		Decoder.Result<?> decoderResult = null;
//		try {
//			decoderResult = engine.decode(in, out, false);
//		} catch (ProtocolException e) {
//			return;
//		}
//		out.flip();
//		fieldDecoder.addData(out);
//		if (decoderResult.isOverflow()) {
//			continue;
//		}
//		if (decoderResult.isUnderflow()) {
//			in.clear();
//			channel.read(in);
//			in.flip();
//			continue;
//		}
//		break;
//	}
//	response.setStatus(HttpStatus.OK);
//	response.setMessageHasBody(true);
//	response.setField(HttpField.CONTENT_TYPE,
//			MediaType.builder().setType("text", "plain")
//			.setParameter("charset", "utf-8").build());
//	String data = "First name: " + fieldDecoder.fields().get("firstname")
//	        + "\r\n" + "Last name: "
//	        + fieldDecoder.fields().get("lastname");
//	ByteBuffer body = ByteBuffer.wrap(data.getBytes("utf-8"));
//	sendResponse(response, body, true);
{'fraction_non_alphanumeric': 0.12687441424554827, 'fraction_numerical': 0.0016401124648547328, 'mean_word_length': 4.620144832126399, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '377828', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2749, 'n_words': 594}