//Find a specific arg in a string of get- or post-data.
//Line is the string of post/get-data, arg is the name of the value to find. The
//zero-terminated result is written in buff, with at most buffLen bytes used. The
//function returns the length of the result, or -1 if the value wasn't found.
int ICACHE_FLASH_ATTR httpdFindArg(char *line, char *arg, char *buff, int buffLen) {
	char *p, *e;
	if (line==NULL) return 0;
	p=line;
	while(p!=NULL && *p!='\n' && *p!='\r' && *p!=0) {
		os_printf("findArg: %s\n", p);
		if (os_strncmp(p, arg, os_strlen(arg))==0 && p[strlen(arg)]=='=') {
			p+=os_strlen(arg)+1; //move p to start of value
			e=(char*)os_strstr(p, "&");
			if (e==NULL) e=p+os_strlen(p);
			os_printf("findArg: val %s len %d\n", p, (e-p));
			return httpdUrlDecode(p, (e-p), buff, buffLen);
		}
		p=(char*)os_strstr(p, "&");
		if (p!=NULL) p+=1;
	}
	os_printf("Finding %s in %s: Not found :/\n", arg, line);
	return -1; //not found
}
Exemple #2
0
/*	httpdFindArg	*/
int ICACHE_FLASH_ATTR	httpdFindArg(char *	pszLine,
									 char *	pszArgument,
									 char *	pszBuffer,
									 int 	cbBufferLength)
{
	/* initialization */
	int 	iRet 	 			= -1;
	char *	pbBeginningOfArg	= NULL;
	char *	pbEndOfArg 			= NULL;

	if (NULL == pszLine)
	{
		iRet = 0;
		goto lblCleanup;
	}

	pbBeginningOfArg = pszLine;

	while ((NULL_STRING != pbBeginningOfArg) 				&& 
		  ((*NEWLINE_SYMBOL_STRING) != *pbBeginningOfArg) 	&& 
		  ((*NEWLINE_R_SYMBOL_STRING) != *pbBeginningOfArg)	&& 
		  (NULL_STRING != *pbBeginningOfArg))
	{

#ifdef HTTPD_DEBUG
		os_printf("httpdFindArg: %s\n", pbBeginningOfArg);
#endif
		if ((0 == os_strncmp(pbBeginningOfArg, pszArgument, os_strlen(pszArgument))) && ((*EQUAL_SYMBOL_STRING) == pbBeginningOfArg[strlen(pszArgument)]))
		{
			pbBeginningOfArg += os_strlen(pszArgument) + 1; //move pbBeginningOfArg to start of value
			pbEndOfArg = (char *)os_strstr(pbBeginningOfArg, AMPERSAND_SYMBOL_STRING);
			
			if (NULL == pbEndOfArg)
			{
				pbEndOfArg = pbBeginningOfArg + os_strlen(pbBeginningOfArg);
			}

#ifdef HTTPD_DEBUG
			os_printf("httpdFindArg: value: %s length: %d\n", pbBeginningOfArg, (pbEndOfArg - pbBeginningOfArg));
#endif
			
			iRet = httpdUrlDecode(pbBeginningOfArg, (pbEndOfArg - pbBeginningOfArg), pszBuffer, cbBufferLength);
			goto lblCleanup;
		}

		pbBeginningOfArg = (char *)os_strstr(pbBeginningOfArg, AMPERSAND_SYMBOL_STRING);
		if (NULL != pbBeginningOfArg)
		{
			pbBeginningOfArg += 1;
		}
	}

	/* not found */	

#ifdef HTTPD_DEBUG
	os_printf("httpdFindArg: didn't find %s in %s\n", pszArgument, pszLine);
#endif

lblCleanup:
	return iRet;
}