Example #1
0
static void esif_ws_http_send_401 (int fd)
{
	char buffer[256];
	char tmpbuffer[128];
	char *fileType = "text/html";
	FILE *file_fp  = NULL;
	int len;
	struct stat st;
	int ret;


	char *fileName = "error.html";

	printf("fileName :%s\n", fileName);
	esif_ccb_fopen(&file_fp, fileName, "r");

	if (NULL == file_fp) {
		printf("failed to open file: \n");
		return;
	}

	if (esif_ccb_stat(fileName, &st) != 0) {
		printf("Could not stat file descriptor \n");
		return;
	}

	len = (long)fseek(file_fp, (off_t)0, SEEK_END);

	(void)fseek(file_fp, (off_t)0, SEEK_SET);

	(void)esif_ccb_sprintf(256, buffer, (char*)"HTTP/1.1 401 Unauthorized\n"
											   "Server: server/%d.0\nLast-Modified: %s\nDate: %s\n"
											   "Content-Type: %s\nContent-Length: %ld\nConnection: close\n\n",
						   VERSION, esif_ws_http_time_stamp(st.st_mtime, tmpbuffer), esif_ws_http_time_stamp(time(0), tmpbuffer), fileType, (long)st.st_size);

	(void)send(fd, buffer, (int)esif_ccb_strlen(buffer), 0);

	while ((ret = (int)fread(buffer, 1, 256, file_fp)) > 0)
		(void)send(fd, buffer, ret, 0);

	fclose(file_fp);
}
Example #2
0
static int esif_ws_http_server_static_pages (
	char *buffer,
	char *resource,
	int fd,
	ssize_t ret,
	char *fileType
	)
{
	struct stat st;
	char tmpbuffer[128];
	char file_to_open[1000];
	FILE *file_fp = NULL;
	#define MAX_SIZE 200


	esif_ccb_memcpy(file_to_open, g_server_root, esif_ccb_strlen(g_server_root, MAX_SIZE));
	file_to_open[esif_ccb_strlen(g_server_root, MAX_SIZE)] = '\0';
	// printf("file to open after esif_ccb_memcpy: %s\n", file_to_open);
	// printf("resource : %s\n", resource);

#ifdef ESIF_ATTR_OS_WINDOWS

	strcat_s((esif_string)file_to_open, 1000, resource);
	esif_ccb_fopen(&file_fp, (esif_string)file_to_open, (esif_string)"rb");
#else
	strcat((esif_string)file_to_open, resource);
	esif_ccb_fopen(&file_fp, (esif_string)file_to_open, (esif_string)"r");
#endif
	printf("file to open: %s\n", file_to_open);
	printf("file type: %s\n", fileType);


	if (NULL == file_fp) {
		printf("failed to open file: %s\n", file_to_open);
		return 404;
	}

	if (esif_ccb_stat(file_to_open, &st) != 0) {
		printf("Could not stat file descriptor \n");
		fclose(file_fp);
		return 404;
	}


	(long)fseek(file_fp, (off_t)0, SEEK_END);
	(void)fseek(file_fp, (off_t)0, SEEK_SET);

	if (!strcmp(fileType, "text/xml")) {
		(void)esif_ccb_sprintf(BUFFER_LENGTH, buffer, (char*)"HTTP/1.1 200 OK\n<?xml version==\"1.0\" encoding=\"utf-8\"?>\n"
															 "Server: server/%d.0\n"
															 "Content-Type: %s\nContent-Length: %ld\nConnection: close\n\n",
							   VERSION, fileType, (long)st.st_size);
	} else {
		(void)esif_ccb_sprintf(BUFFER_LENGTH, buffer, (char*)"HTTP/1.1 200 OK\n"
															 "Server: server/%d.0\nLast-Modified: %s\nDate: %s\n"
															 "Content-Type: %s\nContent-Length: %ld\nConnection: close\n\n",
							   VERSION,
							   esif_ws_http_time_stamp(st.st_mtime, tmpbuffer), esif_ws_http_time_stamp(time(0), tmpbuffer), fileType, (long)st.st_size);
	}

	(void)send(fd, buffer, (int)esif_ccb_strlen(buffer, MAX_SIZE), 0);
	while ((ret = (int)fread(buffer, 1, BUFFER_LENGTH, file_fp)) > 0)

		(void)send(fd, buffer, ret, 0);
	fclose(file_fp);

	return 0;
}
Example #3
0
static int esif_ws_http_process_static_pages (
	ClientRecordPtr connection,
	char *buffer,
	char *resource,
	ssize_t ret,
	char *fileType
	)
{
	struct stat st={0};
	char tmpbuffer[128]={0};
	char file_to_open[MAX_PATH]={0};
	char content_disposition[MAX_PATH]={0};
	FILE *file_fp = NULL;

	esif_build_path(file_to_open, sizeof(file_to_open), ESIF_PATHTYPE_UI, resource, NULL);
	esif_ccb_fopen(&file_fp, (esif_string)file_to_open, (esif_string)"rb");
	
	// Log file workaround: If not found in HTML folder, look in LOG folder
	if (NULL == file_fp) {
		char logpath[MAX_PATH]={0};
		esif_build_path(logpath, sizeof(logpath), ESIF_PATHTYPE_LOG, resource, NULL);
		esif_ccb_fopen(&file_fp, (esif_string)logpath, (esif_string)"rb");
		if (NULL != file_fp) 
			esif_ccb_strcpy(file_to_open, logpath, sizeof(file_to_open));
	}
	
	ESIF_TRACE_DEBUG("HTTP: file=%s, type=%s\n", file_to_open, fileType);
	if (NULL == file_fp) {
		ESIF_TRACE_DEBUG("failed to open file: %s\n", file_to_open);
		return 404;
	}

	if (esif_ccb_stat(file_to_open, &st) != 0) {
		ESIF_TRACE_DEBUG("Could not stat file descriptor \n");
		fclose(file_fp);
		return 404;
	}

	fseek(file_fp, (off_t)0, SEEK_END);
	fseek(file_fp, (off_t)0, SEEK_SET);

	// Add Content-Disposition header to prompt user with Save-As Dialog if unknown file type
	if (esif_ccb_strcmp(fileType, UNKNOWN_MIME_TYPE) == 0) {
		esif_ccb_sprintf(sizeof(content_disposition), content_disposition,  "Content-Disposition: attachment; filename=\"%s\";\n", resource);
	}

	esif_ccb_sprintf(WS_BUFFER_LENGTH, buffer,	
					"HTTP/1.1 200 OK\n"
					"Server: ESIF_UF/%s\n"
					"Last-Modified: %s\n"
					"Date: %s\n"
					"Content-Type: %s\n"
					"Content-Length: %ld\n"
					"%s"
					"Connection: close\n"
					"\n",
				ESIF_UF_VERSION,
				esif_ws_http_time_stamp(st.st_mtime, tmpbuffer),
				esif_ws_http_time_stamp(time(0), tmpbuffer), 
				fileType, 
				(long)st.st_size, 
				content_disposition);

	send(connection->socket, buffer, (int)esif_ccb_strlen(buffer, WS_BUFFER_LENGTH), ESIF_WS_SEND_FLAGS);
	while ((ret = (int)fread(buffer, 1, WS_BUFFER_LENGTH, file_fp)) > 0) {
		send(connection->socket, buffer, (int)ret, ESIF_WS_SEND_FLAGS);
	}
	fclose(file_fp);

	return 0;
}