Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added userData to allow user to store context variable. #34

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

valkuc
Copy link
Contributor

@valkuc valkuc commented Nov 29, 2016

cgiData cannot be used for this purpose in templates (because it's used internally), new userData can - it's dedicated for this.

cgiData cannot be used for this purpose in templates, new userData can - it's dedicated for this.
@valkuc
Copy link
Contributor Author

valkuc commented Nov 29, 2016

Example usage:

void ICACHE_FLASH_ATTR cgi_device_manage_tpl(HttpdConnData *connData, char *token, void **arg)
{
	char buf[128];

	manage_state_t *state = connData->userData;

	if (token == NULL)
	{
		// called at the end of template processing
		if (state != NULL) os_free(state);
		return;
	}

	if (state == NULL)
	{
		state = os_malloc(sizeof(manage_state_t));
		if (state == NULL)
		{
			LOGN(LOG_ERR, "Can't allocate state struct.");
			return;
		}
		os_memset(state, 0, sizeof(manage_state_t));

		int len = httpdFindArg(connData->getArgs, "id", buf, sizeof(buf));
		if (len > 0)
		{
			state->id = atoi(buf);
			//Read state->dev from some storage
		}
		else
		{
			state->dev.active = true;

			//Fill other state->dev fields here
		}

		connData->userData = state;
	}

	if (os_strcmp(token, "foo") == 0)
	{
		os_strcpy(buf, "bar");
	}
	else if (os_strcmp(token, "bar") == 0)
	{
		os_strcpy(buf, "foo");
	}
	//
	// more if else blocks
	//
	else
	{
		os_strcpy(buf, "");
	}

	httpdSend(connData, buf, -1);
}

sonofusion82 and others added 12 commits January 1, 2017 18:32
Caused by buffer overflow.
SDK 2.x provides prototypes for some of the functions listed here, but with
slightly different argument types, leading to build errors:

In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:17:6: error: conflicting types for 'ets_install_putc1'
 void ets_install_putc1(void *routine);
      ^
In file included from ./include/esp8266.h:27:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/osapi.h:34:6: note: previous declaration of 'ets_install_putc1' was here
 void ets_install_putc1(void (*p)(char c));
      ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:18:6: error: conflicting types for 'ets_isr_attach'
 void ets_isr_attach(int intr, void *handler, void *arg);
      ^
In file included from ./include/esp8266.h:24:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/ets_sys.h:67:6: note: previous declaration of 'ets_isr_attach' was here
 void ets_isr_attach(int i, ets_isr_t func, void *arg);
      ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:28:8: error: conflicting types for 'ets_strlen'
 size_t ets_strlen(const char *s);
        ^
In file included from ./include/esp8266.h:27:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/osapi.h:47:5: note: previous declaration of 'ets_strlen' was here
 int ets_strlen(const char *s);
     ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:29:5: error: conflicting types for 'ets_strncmp'
 int ets_strncmp(const char *s1, const char *s2, int len);
     ^
In file included from ./include/esp8266.h:27:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/osapi.h:48:5: note: previous declaration of 'ets_strncmp' was here
 int ets_strncmp(const char *s1, const char *s2, unsigned int n);
     ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:32:6: error: conflicting types for 'ets_timer_arm_new'
 void ets_timer_arm_new(os_timer_t *a, int b, int c, int isMstimer);
      ^
In file included from ./include/esp8266.h:27:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/osapi.h:65:6: note: previous declaration of 'ets_timer_arm_new' was here
 void ets_timer_arm_new(os_timer_t *ptimer, uint32_t time, bool repeat_flag, bool ms_flag);
      ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:40:6: error: conflicting types for 'uart_div_modify'
 void uart_div_modify(int no, unsigned int freq);
      ^
In file included from ./include/esp8266.h:28:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/user_interface.h:646:6: note: previous declaration of 'uart_div_modify' was here
 void uart_div_modify(uint8 uart_no, uint32 DivLatchValue);
      ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:45:6: error: conflicting types for 'ets_delay_us'
 void ets_delay_us(int ms);
      ^
In file included from ./include/esp8266.h:27:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/osapi.h:33:6: note: previous declaration of 'ets_delay_us' was here
 void ets_delay_us(uint16_t us);
      ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:57:7: error: conflicting types for 'pvPortMalloc'
 void *pvPortMalloc(size_t xWantedSize, const char *file, int line);
       ^
In file included from ./include/esp8266.h:26:0,
                 from espfs/espfs.c:23:
/home/peko/source/esp8266/esp-open-sdk/sdk//include/mem.h:38:7: note: previous declaration of 'pvPortMalloc' was here
 void *pvPortMalloc (size_t sz, const char *, unsigned);
       ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:58:7: error: conflicting types for 'pvPortZalloc'
 void *pvPortZalloc(size_t, const char *file, int line);
       ^
In file included from ./include/esp8266.h:26:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/mem.h:40:7: note: previous declaration of 'pvPortZalloc' was here
 void *pvPortZalloc (size_t sz, const char *, unsigned);
       ^
In file included from ./include/esp8266.h:33:0,
                 from espfs/espfs.c:23:
./include/espmissingincludes.h:59:6: error: conflicting types for 'vPortFree'
 void vPortFree(void *ptr, const char *file, int line);
      ^
In file included from ./include/esp8266.h:26:0,
                 from espfs/espfs.c:23:
/opt/esp-open-sdk/sdk/include/mem.h:39:6: note: previous declaration of 'vPortFree' was here
 void vPortFree (void *p, const char *, unsigned);
      ^

Adjust the prototypes here to match the SDK to fix these issues.

Signed-off-by: Peter Korsgaard <[email protected]>
cgiData cannot be used for this purpose in templates, new userData can - it's dedicated for this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants