How to create a global variable G-Wan correctly?

--- config.h

extern char userurl[3];
char userurl[3];

      

--- index.c

include "config.h"

int main(int argc, char *argv[]) {  
    char *req_g="",*req_p="";

    get_arg("g=", &req_g, argc,argv);
    get_arg("p=", &req_p, argc,argv);

    strcat(userurl,req_g);
    strcat(userurl,req_p);
    ..

    xbuf_xcat(reply,"%s",userurl);
    ..

    return 200;
}

      

Then I used http://127.0.0.1:8080/?index&g=a&p=b

I reload multiple times and duplicate results: userurl is not freed ...

What's the correct way to declare variables extern

or global

for gwan?

+3


source to share


1 answer


Each G-WAN script is compiled separately. As a result, all your variables are static (local to this module) - you cannot share them without using pointers and atomic operations.

To facilitate the use of global variables, G-WAN provides persistent pointers ( US_HANDLER_DATA

, US_VHOST_DATA

or US_REQUEST_DATA

):



void *pVhost_persistent_ptr = (void*)get_env(argv, US_VHOST_DATA);
if(pVhost_persistent_ptr)
   printf("%.4s\n", pVhost_persistent_ptr);

// get a pointer on a pointer (to CHANGE the pointer value)
void **pVhost_persistent_ptr = (void*)get_env(argv, US_VHOST_DATA);
if(pVhost_persistent_ptr)
   *pVhost_persistent_ptr = strdup("persistent data");

      

Several examples, such as persistence.c or stream3.c, illustrate how to operate with real programs.

0


source







All Articles