void escrever_buffer(char string[], int tam_string){
	if(tam_string + tam_buffer > MAXLINE)
		enviar_buffer();
	
	strcat(buffer, string);
	tam_buffer += tam_string;
}
Beispiel #2
0
void enviar_mensaje_con_buffer_al_UMC(int socket_umc, int id_mensaje, t_buffer *t_buffers) {
	t_header *header = malloc(sizeof(t_header));
	header->id_proceso_emisor = PROCESO_SWAP;
	header->id_proceso_receptor = PROCESO_UMC;

	header->id_mensaje = id_mensaje;
	header->longitud_mensaje = t_buffers->longitud_buffer;

	int cantidad_bytes_enviados = enviar_buffer (socket_umc, header, t_buffers);

	if (cantidad_bytes_enviados < sizeof(t_header))
		log_error(loggerManager,"[Comunicacion UMC] [%i] Ocurrió un problema al enviar el mensaje", id_mensaje);
	else
		log_trace(loggerManager,"[Comunicacion UMC] [%i] Se realizo el envio del mensaje correctamente", id_mensaje);

	free(header);
}
void enviar_conteudo_post(){
	char linha[MAXLINE + 1];
	sprintf(linha,  "Você preencheu um formulário de %d campo(s): \r\n\r\n", qtd_campos_post);
	escrever_buffer(linha, 49);

	escrever_buffer("+--------------------+--------------------+\r\n", 45);
	escrever_buffer("|      Campo         |        Valor       |\r\n", 45);
	escrever_buffer("+--------------------+--------------------+\r\n", 45);
	
	int i;
	for(i = 0; i < qtd_campos_post; i++){
		sprintf(linha, "|%-20s|%-20s|\r\n", chaves_post[i], valores_post[i]);
		escrever_buffer(linha, 45);
	}
	
	escrever_buffer("+--------------------+--------------------+\r\n", 45);
	enviar_buffer();
}
void enviar_resposta(resposta resp, tipo_requisicao tipo){
	char tmp[MAXLINE + 1];
	char *protocolo, *nome_status;
	int tam_tmp;
	
	buffer[0] = '\0';
	tam_buffer = 1;
	
	protocolo = "";
	if(resp.protocolo == HTTP10)
		protocolo = "HTTP/1.0";
	else if(resp.protocolo == HTTP11)
		protocolo = "HTTP/1.1";
	
	nome_status = "";
	if(resp.status == 200)
		nome_status = "OK";
	else if(resp.status == 404)
		nome_status = "Not found";

	//HTTP/1.1 200 OK	
	tam_tmp = snprintf(tmp, sizeof(tmp), "%s %d %s\r\n", protocolo, resp.status, 
						nome_status);
	escrever_buffer(tmp, tam_tmp);
	
	
	//Date:	Sun, 26 Aug 2012 13:12:48 GMT
	time_t agora = time(0);
	struct tm tm = *gmtime(&agora);
	tam_tmp = strftime(tmp, sizeof(tmp), "Date: %a, %d %b %Y %H:%M:%S %Z\r\n", &tm);
	escrever_buffer(tmp, tam_tmp);
	
	//Server: Apache/2.2.16
	tam_tmp = snprintf(tmp, sizeof(tmp), "Server: %s\r\n", NOME_SERVIDOR);
	escrever_buffer(tmp, tam_tmp);
	
	
	struct stat infos;
	int leu_infos_arquivo = stat(resp.arquivo, &infos) == 0 ? 1 : 0;
	if (tipo == GET && resp.status == 200 && leu_infos_arquivo) {
		//Last-Modified: Sun, 31 Jul 2011 17:26:08 GMT
		tam_tmp = strftime(tmp, sizeof(tmp), "Last-Modified: %a, %d %b %Y %H:%M:%S %Z\r\n", 
							localtime( &infos.st_mtime));
		escrever_buffer(tmp, tam_tmp);
		
		//ETag:	"21838f9-1ed-4a960cf6e557a"
		tam_tmp = snprintf(tmp, sizeof(tmp), "ETag: \"%d\"\r\n", (int) infos.st_mtime);
		escrever_buffer(tmp, tam_tmp);
		
		//Accept-Ranges:	bytes
		tam_tmp = snprintf(tmp, sizeof(tmp), "Accept-Ranges: bytes\r\n");
		escrever_buffer(tmp, tam_tmp);
		
		//Content-Length:	349
		tam_tmp = snprintf(tmp, sizeof(tmp), "Content-Length: %d\r\n", (int) infos.st_size);
		escrever_buffer(tmp, tam_tmp);
	}
	
	//Vary:	Accept-Encoding
	tam_tmp = snprintf(tmp, sizeof(tmp), "Vary: Accept-Encoding\r\n");
	escrever_buffer(tmp, tam_tmp);

	//Connection: close
	tam_tmp = snprintf(tmp, sizeof(tmp), "Connection: close\r\n");
	escrever_buffer(tmp, tam_tmp);
	
	//Content-Type: text/html
	char *content_type = "text/plain";
	if(tipo == GET)
		content_type = obter_content_type(resp.arquivo);
	tam_tmp = snprintf(tmp, sizeof(tmp), "Content-Type: %s\r\n", content_type);
	escrever_buffer(tmp, tam_tmp);
	
	escrever_buffer("\r\n", 2);
	enviar_buffer();
}