#include "stdafx.h"
#include "classdef.h"
#include "winsock2.h"
#include "constdef.h"
#include <sys/stat.h>
#include <map>
using namespace std;

void sendHead(SOCKET s, int state, char* fileExtension, char* version);
bool sendContent(SOCKET s, char* filePath);
map<string, string> map_param;

char ascii_string[10000];
char *char_to_ascii(char ch){
	char *ascii_str;
	ascii_string[0] = 0;
	ascii_str = ascii_string;
	if(isgraph(ch)){
		*ascii_str++ = ch;
	}else if(ch == ' '){
		*ascii_str++ =ch;
	}else if(ch == '\n' || ch == '\r'){
		*ascii_str++ =ch;
	}else{
		*ascii_str++ ='.';
	}
	*ascii_str = 0;
	return ascii_string;
}

void parse_interface(){
	string username, inter_face, json_data, version; 
	map<string, string>::iterator it_para = map_param.begin();
	for(; it_para != map_param.end(); ++it_para){
		if(it_para->first == PARAM_NAME_USERNAME){
			username = it_para->second;
		}else if(it_para->first == PARAM_NAME_INTERFACE){
			inter_face = it_para->second;
		}else if(it_para->first == PARAM_NAME_DATA){
			json_data = it_para->second;
		}else if(it_para->first == PARAM_NAME_VERSION){
			version = it_para->second;
		}
	}
	//
	if(version == "1.0"){
		if(inter_face == INTERFACE_NAME_SETLIMITVALUE){

		}else if(inter_face == INTERFACE_NAME_CALLCARD){

		}else if(inter_face == INTERFACE_NAME_CLEARCARD){

		}else if(inter_face == INTERFACE_NAME_EDITAREA){

		}else if(inter_face == INTERFACE_NAME_EDITSTATION){

		}else if(inter_face == INTERFACE_NAME_EDITPATH){

		}else if(inter_face == INTERFACE_NAME_EDITCARD){

		}else if(inter_face == INTERFACE_NAME_EDITSTAFFER){

		}else if(inter_face == INTERFACE_NAME_EDITVEHICLE){

		}
	}
}

void parse_param_data(char content[], int number){
	//char temp[1000];
	int i,j = 0;
	bool bkey = true;
	string para_key = "", para_value = "";

	for(i = 0; i < number; i++){
		if(content[i] == '&'){
			bkey = true;
			para_value += '\0';
			i++;
			map_param.insert(make_pair(para_key, para_value));
			para_key = "";
			para_value = "";
			j = 0;
		}
		if(content[i] == '='){
			bkey = false;
			para_key += '\0';
			i++;
			j = 0;
		}		
		if(bkey){
			para_key += content[i];
		}else{
			para_value += content[i];
		}
		j++;
	}
	para_value += '\0';
	map_param.insert(make_pair(para_key, para_value));
}

void parse_server_data(char content[], size_t number){
	char temp[1024];
	char str1[1024];
	char str2[1024];
	char str3[1024];
	char paralen[20];
	size_t i, j, k = 0, m = 0;
	char entity_content[1024];

	for(i = 0; i < number; i++){
		if(content[i] != '\n'){
			k++;
			continue;
		}
		for(j = 0; j < k; j++)
			temp[j] = content[j + i - k];
		temp[j] = '\0';
		if(strstr(temp, "GET") || strstr(temp, "POST")){ // 请求行为
			sscanf_s(temp, "%s %s %s", str1, str2, str3); // 命令，路径，协议版本
		}
		//if(strstr(temp, "Accept:")){ // 数据文件包括(Accept:)

		//}
		//if(strstr(temp, "Referer")){ // 转移地址（Referer）

		//}
		//if(strstr(temp, "Accept-Language")){ // 使用的语言 

		//}
		//if(strstr(temp, "Accept-Encoding")){ // 编码方式

		//}
		//if(strstr(temp, "If-Modified-Since")){ // 上次修改时间

		//}
		//if(strstr(temp, "If-Node-Match")){ // 

		//}
		//if(strstr(temp, "User-Agent")){ // 用户浏览器信息

		//}
		//if(strstr(temp, "Host")){ // 访问的主机

		//}
		//if(strstr(temp, "Connection")){ // 连接状态

		//}
		//if(strstr(temp, "Cookie")){ // cookie

		//}
		//if(strstr(temp, "Cache-Control:")){ // 

		//}
		//if(strstr(temp, "Content-Type:")){ // 实体数据格式
		//
		//}
		if(strstr(temp, "Content-Length:")){ // 数据长度
			//sscanf(temp, "%s%d", temp, paralen);
			m = 0;
			for(j = 15; j < strlen(temp); j++){
				if(temp[j] != ' ' && temp[j] != '\n'){
					paralen[m] = temp[j];
					m++;
				}
			}
			paralen[m] = '\0';
			m = atoi(paralen);
		}
		// 实体数据
		if((content[i]  == '\n') && content[i + 1] == '\r' && content[i + 2] == '\n'){
			if(i + 3 == strlen(content)){ // 无数据
				break;
			}
			//for (j = 0; j < strlen(content) - i - 3; j++){
			for(j = 0; j < m; j++){
				entity_content[j] = content[i + 3 + j];
			}
			entity_content[j] = '\0';

			string estr = entity_content;
			// 解析参数
			parse_param_data(entity_content, m);
			break;
		}
		k = 0;		
	}
}

DWORD WINAPI clientThread(LPVOID lparam)
{
	PARA* para = (PARA*)lparam;
	char buf[MAXSIZE], filePath[200], cmd[50], version[50], fileExtension[50]={'\0'};
	int recvCount;

	filePath[0]='.';
	//int aa, bb, cc = 0;
	//char temp1[200], temp2[200] = {0};
	recvCount = recv(para->s, buf, MAXSIZE, 0);
	sscanf_s(buf, "%s %s %s", cmd, filePath + 1, version);

	parse_server_data(buf, recvCount);

	closesocket(para->s);
	delete para;
	return 0;
}