Пример #1
0
// This translation engine can not process <%...%> interlace with <#...#>
void
do_ej(char *path, FILE *stream)
{
#define PATTERN_LENGTH  1024
#define FRAG_SIZE	       128
#define RESERVE_SIZE    4
	int frag_size = FRAG_SIZE;
	int pattern_size = PATTERN_LENGTH - RESERVE_SIZE;
	char pat_buf[PATTERN_LENGTH];
	char *pattern = pat_buf, *asp = NULL, *asp_end = NULL, *key = NULL, *key_end = NULL;
	char *start_pat, *end_pat, *lang;
	FILE *fp;
	int conn_break = 0;
	size_t ret, read_len, len;
	int no_translate = 1;
	unsigned char lang_mib[16];	//2011.04.12 Jerry

	if (!(fp = fopen(path, "r")))
		return;

#ifdef TRANSLATE_ON_FLY
	// Load dictionary file
	// If the router is restored to default, using browser's language setting to display ALL pages
	if (is_firsttime () && Accept_Language[0] != '\0')      {
		lang = Accept_Language;
	} else {
		apmib_get( MIB_PREFERRED_LANG, (void *)&lang_mib);
		lang = lang_mib;
	}

	if (load_dictionary (lang, &kw))	{
		no_translate = 0;
	}
#endif  //defined TRANSLATE_ON_FLY
	start_pat = end_pat = pattern;
	memset (pattern + pattern_size, 0, 4);
	while (conn_break == 0)
	{
		int special;

		// Arrange pattern[] if available buffer length (end_pat~pattern[pattern_size]) is smaller than frag_size
		if (((pattern + pattern_size) - end_pat) < frag_size)
		{
			len = end_pat - start_pat;
			memcpy (pattern, start_pat, len);
			start_pat = pattern;
			end_pat = start_pat + len;
			*end_pat = '\0';
		}

		read_len = (pattern + pattern_size) - end_pat;
		len = fread (end_pat, 1, read_len, fp);
		if (len == 0)   {
			if (start_pat < end_pat)	{
				fwrite (start_pat, 1, (size_t) (end_pat - start_pat), stream);
			}
			break;
		}
		end_pat += len;
		*end_pat = '\0';


		asp = strstr (start_pat, asp_mark1);
		key = NULL;
		if (no_translate == 0)  {
			key = strstr (start_pat, kw_mark1);
		}
		special = 0;
		while ((start_pat < end_pat) && special == 0)
		{
			int postproc = 0;       /* 0: need more data; 1: translate; 2: execute asp; 3: write only; */
			char *s, *e, *p;

			/*				 asp      asp_end
			 *				 ^	^
			 *      +------------------------------<%.......%>-------------------------------+
			 *  |	 XXXXXXXXXXXXXXXXXXXXX<#.......#>YYYYYYYYYYYYYYYYYY0	    |0000
			 *  +------------------------------------------------------------------------+
			 *  ^	 ^		    ^	^ ^		 ^	     ^
			 *  |	 |		    |	| p		 |	     |
			 *  pattern   start_pat,s	  key,e(2) key_end	     end_pat,e(1)  pattern + pattern_size
			 *				     ^				|
			 *				     +--------------------------------+
			 *
			 */

			// If <%...%> and <#...#> do not exist in pattern[], write whole pattern[].
			s = start_pat;
			e = end_pat;

			if (key != NULL && asp == NULL) {
				e = key;						// Write start_pat ~ (key - 1)
				key_end = strstr (key, kw_mark2);
				if (key_end != NULL)    {	       // We do have <#...#> in pattern[].
					postproc = 1;
				}
			} else if (key != NULL && asp != NULL)  {
				// We have <%...%> and <#...#> in pattern[], process first occurrence
				if (asp < key)  {
					e = asp;					// Write start_pat ~ (asp - 1)
					asp_end = strstr (asp, asp_mark2);
					if (asp_end != NULL)    {       // We do have whole <%...%>.
						postproc = 2;
					}
				} else {
					e = key;					// Write start_pat ~ (key - 1)
					key_end = strstr (key, kw_mark2);
					if (key_end != NULL)    {       // We do have whole <#...#>.
						postproc = 1;
					}
				}
			} else if (key == NULL && asp != NULL)  {
				e = asp;						// Write start_pat ~ (asp - 1)
				asp_end = strstr (asp, asp_mark2);
				if (asp_end != NULL)    {	       // We do have whole <%...%>.
					postproc = 2;
				}
			} else {
				// Special case. If last character is '<'
				// DO NOT write this character due to next one may be % or #.
				if (*(e-1) == *asp_mark1 || *(e-1) == *kw_mark1)	{
					special = 1;
					e--;
				}

				postproc = 3;
			}

			// process text preceeding <# or <%
			if (e > s)      {
				ret = fwrite (s, 1, (size_t) (e - s), stream);
				if (ret == 0 || ret < (e - s))  {
					/* the connection had been damaged. DO NOT process another data. */
					/* (reduce response time of httpd) */
//				      cprintf ("fwrite() ret %d, s %p e %p len %d, break do_ej()'s while loop\n", ret, s, e, e-s);
					conn_break = 1;
					break;
				} else {
					start_pat = e;
				}
			}
			// post process
			p = NULL;
			if (postproc == 1) {			    // translate
				p = translate_lang (key + strlen (kw_mark1), key_end, stream, &kw);
				if (no_translate == 0 && p != NULL)     {
					key = strstr (p, kw_mark1);
				}
			} else if (postproc == 2)       {	       // execute asp
				p = process_asp (asp + strlen (asp_mark1), asp_end, stream);
				if (p != NULL)  {
					asp = strstr (p, asp_mark1);
				}
			} else if (postproc == 3)       {	       // no <%...%> or <#...#>
				p = e;
			} else if (postproc == 0)       {	       // read more data
				break;
			}

			if (p != NULL)  {
				start_pat = p;
			}

		}       /* while ((start_pat < end_pat) && special == 0) */
	}	       /* while (conn_break == 0) */


	fflush (stream);
	fclose(fp);
	system("/bin/clear_cache.sh");
}
Пример #2
0
Файл: ej.c Проект: PterX/rt-n56u
// This translation engine can not process <%...%> interlace with <#...#>
void
do_ej(const char *url, FILE *stream)
{
#define PATTERN_LENGTH	1024
#define FRAG_SIZE	128
#define RESERVE_SIZE	4
	FILE *fp;
	int frag_size = FRAG_SIZE;
	int pattern_size = PATTERN_LENGTH - RESERVE_SIZE;
	char pat_buf[PATTERN_LENGTH];
	char *pattern = pat_buf, *asp = NULL, *asp_end = NULL, *key = NULL, *key_end = NULL;
	char *start_pat, *end_pat, *lang;
	pkw_t pkw = &kw_EN;
	int conn_break = 0;
	size_t ret, read_len, len;
	int no_translate = 1;

	if (!(fp = fopen(url, "r")))
		return;

	// Load dictionary file
	lang = nvram_safe_get("preferred_lang");
	if (strlen(lang) > 1 && strcmp(lang, "EN") != 0) {
		if (strcmp(lang, kw_XX.dict) != 0) {
			release_dictionary(&kw_XX);
			if (load_dictionary(lang, &kw_XX))
				pkw = &kw_XX;
		} else
			pkw = &kw_XX;
	} else {
		if (kw_XX.buf)
			release_dictionary(&kw_XX);
	}

	if (pkw->buf)
		no_translate = 0;

	start_pat = end_pat = pattern;
	memset (pattern + pattern_size, 0, 4);
	while (conn_break == 0)
	{
		int special;

		// Arrange pattern[] if available buffer length (end_pat~pattern[pattern_size]) is smaller than frag_size
		if (((pattern + pattern_size) - end_pat) < frag_size)
		{
			len = end_pat - start_pat;
			memcpy (pattern, start_pat, len);
			start_pat = pattern;
			end_pat = start_pat + len;
			*end_pat = '\0';
		}

		read_len = (pattern + pattern_size) - end_pat;
		len = fread (end_pat, 1, read_len, fp);
		if (len == 0)   {
			if (start_pat < end_pat){
				fwrite (start_pat, 1, (size_t) (end_pat - start_pat), stream);
			}
			break;
		}
		end_pat += len;
		*end_pat = '\0';

		asp = strstr (start_pat, asp_mark1);
		key = NULL;
		if (no_translate == 0)  {
			key = strstr (start_pat, kw_mark1);
		}
		special = 0;
		while ((start_pat < end_pat) && special == 0)
		{
			int postproc = 0;       /* 0: need more data; 1: translate; 2: execute asp; 3: write only; */
			char *s, *e, *p;

			/*				 asp      asp_end
			 *				 ^	^
			 *      +------------------------------<%.......%>-------------------------------+
			 *  |	 XXXXXXXXXXXXXXXXXXXXX<#.......#>YYYYYYYYYYYYYYYYYY0	    |0000
			 *  +------------------------------------------------------------------------+
			 *  ^	 ^		    ^	^ ^		 ^	     ^
			 *  |	 |		    |	| p		 |	     |
			 *  pattern   start_pat,s	  key,e(2) key_end	     end_pat,e(1)  pattern + pattern_size
			 *				     ^				|
			 *				     +--------------------------------+
			 *
			 */

			// If <%...%> and <#...#> do not exist in pattern[], write whole pattern[].
			s = start_pat;
			e = end_pat;

			if (key != NULL && asp == NULL) {
				e = key;						// Write start_pat ~ (key - 1)
				key_end = strstr (key, kw_mark2);
				if (key_end != NULL)    {	       // We do have <#...#> in pattern[].
					postproc = 1;
				}
			} else if (key != NULL && asp != NULL)  {
				// We have <%...%> and <#...#> in pattern[], process first occurrence
				if (asp < key)  {
					e = asp;					// Write start_pat ~ (asp - 1)
					asp_end = strstr (asp, asp_mark2);
					if (asp_end != NULL)    {       // We do have whole <%...%>.
						postproc = 2;
					}
				} else {
					e = key;					// Write start_pat ~ (key - 1)
					key_end = strstr (key, kw_mark2);
					if (key_end != NULL)    {       // We do have whole <#...#>.
						postproc = 1;
					}
				}
			} else if (key == NULL && asp != NULL)  {
				e = asp;						// Write start_pat ~ (asp - 1)
				asp_end = strstr (asp, asp_mark2);
				if (asp_end != NULL)    {	       // We do have whole <%...%>.
					postproc = 2;
				}
			} else {
				// Special case. If last character is '<'
				// DO NOT write this character due to next one may be % or #.
				if (*(e-1) == *asp_mark1 || *(e-1) == *kw_mark1)	{
					special = 1;
					e--;
				}

				postproc = 3;
			}

			// process text preceeding <# or <%
			if (e > s)      {
				ret = fwrite (s, 1, (size_t) (e - s), stream);
				if (ret == 0 || ret < (e - s))  {
					/* the connection had been damaged. DO NOT process another data. */
					/* (reduce response time of httpd) */
//					cprintf ("fwrite() ret %d, s %p e %p len %d, break do_ej()'s while loop\n", ret, s, e, e-s);
					conn_break = 1;
					break;
				} else {
					start_pat = e;
				}
			}
			// post process
			p = NULL;
			if (postproc == 1) {			    // translate
				p = translate_lang (key + strlen (kw_mark1), key_end, stream, pkw);
				if (no_translate == 0 && p != NULL)     {
					key = strstr (p, kw_mark1);
				}
			} else if (postproc == 2)       {	       // execute asp
				p = process_asp (asp + strlen (asp_mark1), asp_end, stream);
				if (p != NULL)  {
					asp = strstr (p, asp_mark1);
				}
			} else if (postproc == 3)       {	       // no <%...%> or <#...#>
				p = e;
			} else if (postproc == 0)       {	       // read more data
				break;
			}

			if (p != NULL)  {
				start_pat = p;
			}

		}       /* while ((start_pat < end_pat) && special == 0) */
	}	       /* while (conn_break == 0) */

	fclose(fp);

	if (pattern != pat_buf)
		free (pattern);

	fflush(stream);
}