123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <json-c/json.h>
- #include <json-c/json_util.h>
- #include <json-c/json_object.h>
- #include <mosquitto.h>
- #include "common.h"
- #include "command.h"
- static volatile bool run =true;
- char *inputbuffer=NULL;
- void connect_callback(struct mosquitto *mosq, void *obj, int rc)
- {
- run = true;
- }
- void disconnect_callback(struct mosquitto *mosq, void *obj, int result)
- {
- run = false;
- }
- void message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg)
- {
- struct json_object *proot=NULL, *payload_obj=NULL, *item_obj=NULL, *user_obj=NULL, *temp_obj=NULL, *cookied_obj=NULL, *rsp_obj=NULL;
- const char *proot_string=NULL, *rsp_string=NULL;
- const char *user=NULL;
- const char *passwd=NULL;
- const char *load_user=NULL;
- const char *load_passwd=NULL;
- char timestamp[100];
- proot = json_tokener_parse(msg->payload);
- if(proot == NULL)
- {
- printf("ERROR: login proot is NULL!\r\n");
- run =false;
- return ;
- }
- json_object_object_get_ex(proot, "user", &user_obj);
- json_object_object_get_ex(user_obj, "user", &temp_obj);
- user = json_object_get_string(temp_obj);
- json_object_object_get_ex(user_obj, "passwd", &temp_obj);
- passwd = json_object_get_string(temp_obj);
- payload_obj = json_tokener_parse(inputbuffer);
- if(payload_obj == NULL)
- {
- printf("ERROR: inputbuffer is error!\r\n");
- json_object_put(proot);
- run =false;
- return;
- }
- json_object_object_get_ex(payload_obj, "user", &temp_obj);
- load_user = json_object_get_string(temp_obj);
- json_object_object_get_ex(payload_obj, "passwd", &temp_obj);
- load_passwd = json_object_get_string(temp_obj);
- if(strncmp(user,load_user, STR_MAX_LEN(user, load_user)) ==0 &&
- strncmp(passwd, load_passwd,STR_MAX_LEN(passwd,load_passwd)) ==0)
- {
- rsp_obj = json_object_new_object();
- json_object_object_add(rsp_obj,"state", json_object_new_string("success"));
- json_object_object_add(rsp_obj,"user", json_object_new_string(user));
- json_object_object_add(rsp_obj,"passwd", json_object_new_string(passwd));
- timestamp_get(timestamp);
- cookied_obj = json_object_new_object();
- json_object_object_add(cookied_obj, "cookied", json_object_new_string(timestamp));
- json_object_to_file_ext("./cookie/cookied.json", cookied_obj, JSON_C_TO_STRING_PRETTY);
- printf("Set-Cookie:cookied=%s;path=/;",timestamp);
- json_object_put(cookied_obj);
- }
- else{
- rsp_obj = json_object_new_object();
- json_object_object_add(rsp_obj,"state", json_object_new_string("fail"));
- json_object_object_add(rsp_obj,"user", json_object_new_string(load_user));
- json_object_object_add(rsp_obj,"passwd", json_object_new_string(load_passwd));
- }
- printf("%s\n\n","Content-Type:application/json;charset=UTF-8");
- rsp_string = json_object_to_json_string(rsp_obj);
- printf("%s",rsp_string);
- json_object_put(proot);
- json_object_put(payload_obj);
- json_object_put(rsp_obj);
- run =false;
- }
- void subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
- {
- run = true;
- }
- int main(int argc, char *argv[])
- {
- char *info=NULL, *p=NULL, *pRequestMethod=NULL;
- int ContentLength,x,i=0;
- struct mosquitto *http_mosq=NULL;
- int max =0;
- char id[50];
- setvbuf(stdin, NULL , _IONBF, 0);
- pRequestMethod = getenv("REQUEST_METHOD");
- if(pRequestMethod == NULL)
- {
- printf("<p> request = null </p>");
- return 0;
- }
- if(strcasecmp(pRequestMethod, "POST") == 0)
- {
- p = getenv("CONTENT_LENGTH");
- if(p!=NULL)
- {
- ContentLength =atoi(p);
- }
- else
- {
- ContentLength = 0;
- }
- inputbuffer = (char *)malloc(ContentLength + 1);
- while(i < ContentLength)
- {
- x = fgetc(stdin);
- if(x == EOF)
- break;
- *(inputbuffer+i) =x;
- i++;
- }
- *(inputbuffer+i) = '\0';
- ContentLength = i;
- mosquitto_lib_init();
- snprintf(id, 50, "login_%d", getpid());
- http_mosq = mosquitto_new(id, true, NULL);
- mosquitto_connect_callback_set(http_mosq, connect_callback);
- mosquitto_disconnect_callback_set(http_mosq, disconnect_callback);
- mosquitto_message_callback_set(http_mosq, message_callback);
- mosquitto_subscribe_callback_set(http_mosq, subscribe_callback);
- mosquitto_connect(http_mosq, "localhost", 1883, 600);
- mosquitto_subscribe(http_mosq, NULL, SUB_GET_USER_NONE_HTTP, 1);
- mosquitto_publish(http_mosq, NULL, PUB_GET_USER_NONE_HTTP, 0, "", 1, false);
- while(run ==true && max < MAX_TIMEOUT)
- {
- mosquitto_loop(http_mosq, 500, 1);
- max++;
- }
- mosquitto_disconnect(http_mosq);
- mosquitto_destroy(http_mosq);
- mosquitto_lib_cleanup();
- free(inputbuffer);
- }
- return 0;
- }
|