mirror of
https://github.com/lxsang/ant-http
synced 2025-07-24 17:49:46 +02:00
using shared library for common api
This commit is contained in:
@ -18,7 +18,7 @@ void execute(int client,const char* method,dictionary rq)
|
||||
dictionary d = dict();
|
||||
dput(d,"test",c);
|
||||
dput(d,"test1","This is another cookie");
|
||||
set_cookie(client,"text/html; charset=utf-8",d);
|
||||
set_cookie(client,"text/html; charset=utf-8",d,__plugin__.name);
|
||||
|
||||
LOG("%s",c);
|
||||
__t(client,"<h1>Set the cookie</h1>");
|
||||
|
@ -1,3 +1,5 @@
|
||||
#ifndef DB_HELPER
|
||||
#define DB_HELPER
|
||||
#include <sqlite3.h>
|
||||
#include "utils.h"
|
||||
|
||||
@ -31,4 +33,5 @@ char* __name_value_list(const dbfield);
|
||||
char* value_of(const dbfield,const char*);
|
||||
void add_field(dbfield*,const char*, const char*);
|
||||
void add_record(dbrecord*,dbfield);
|
||||
void dbclose(sqlite3*);
|
||||
void dbclose(sqlite3*);
|
||||
#endif
|
217
plugins/handle.c
Normal file
217
plugins/handle.c
Normal file
@ -0,0 +1,217 @@
|
||||
#include "handle.h"
|
||||
|
||||
|
||||
void set_status(int client,int code,const char* msg)
|
||||
{
|
||||
response(client, __s("HTTP/1.0 %d %s", code, msg));
|
||||
response(client, __s("Server: %s ", SERVER_NAME));
|
||||
}
|
||||
void redirect(int client,const char*path)
|
||||
{
|
||||
__t(client,"<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"></head><body></body></html>",path);
|
||||
}
|
||||
|
||||
void html(int client)
|
||||
{
|
||||
ctype(client,"text/html; charset=utf-8");
|
||||
}
|
||||
void text(int client)
|
||||
{
|
||||
ctype(client,"text/plain; charset=utf-8");
|
||||
}
|
||||
void json(int client)
|
||||
{
|
||||
ctype(client,"application/json");
|
||||
}
|
||||
void textstream(int client)
|
||||
{
|
||||
ctype(client, "text/event-stream");
|
||||
}
|
||||
void octstream(int client, char* name)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: application/octet-stream");
|
||||
__t(client,"Content-Disposition: attachment; filename=\"%s\"", name);
|
||||
response(client,"");
|
||||
//Content-Disposition: attachment; filename="fname.ext"
|
||||
}
|
||||
void jpeg(int client)
|
||||
{
|
||||
ctype(client,"image/jpeg");
|
||||
}
|
||||
void ctype(int client, const char* type)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: %s",type);
|
||||
response(client,"");
|
||||
}
|
||||
|
||||
int response(int client, const char* data)
|
||||
{
|
||||
char buf[BUFFLEN+3];
|
||||
strcpy(buf, data);
|
||||
int nbytes;
|
||||
int size = strlen(data);
|
||||
buf[size] = '\r';
|
||||
buf[size+1] = '\n';
|
||||
buf[size+2] = '\0';
|
||||
nbytes = send(client, buf, strlen(buf), 0);
|
||||
return (nbytes ==-1?0:1);
|
||||
}
|
||||
int __ti(int client,int data)
|
||||
{
|
||||
char str[15];
|
||||
sprintf(str, "%d", data);
|
||||
return response(client,str);
|
||||
}
|
||||
|
||||
int __t(int client, const char* fstring,...)
|
||||
{
|
||||
int nbytes;
|
||||
int dlen;
|
||||
int sent = 0;
|
||||
int buflen = 0;
|
||||
va_list arguments;
|
||||
char * data;
|
||||
char* chunk;
|
||||
va_start( arguments, fstring);
|
||||
dlen = vsnprintf(0,0,fstring,arguments) + 1;
|
||||
va_end(arguments);
|
||||
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
||||
{
|
||||
va_start(arguments, fstring);
|
||||
vsnprintf(data, dlen, fstring, arguments);
|
||||
va_end(arguments);
|
||||
|
||||
if(dlen < BUFFLEN)
|
||||
return response(client,data);
|
||||
else
|
||||
{
|
||||
while(sent < dlen - 1)
|
||||
{
|
||||
if(dlen - sent > BUFFLEN)
|
||||
buflen = BUFFLEN;
|
||||
else
|
||||
buflen = dlen - sent - 1;
|
||||
//LOG("BUFFLEN %d\n",buflen);
|
||||
chunk = (char*) malloc((buflen)*sizeof(char));
|
||||
memcpy(chunk,data+sent,buflen);
|
||||
//chunk[buflen-1] = '\0';
|
||||
//response(client,chunk);
|
||||
sent += buflen;
|
||||
nbytes = send(client, chunk, buflen, 0);
|
||||
free(chunk);
|
||||
if(nbytes == -1) return 0;
|
||||
}
|
||||
chunk = "\r\n";
|
||||
send(client, chunk, strlen(chunk), 0);
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
return 1;
|
||||
//
|
||||
}
|
||||
int __b(int client, const unsigned char* data, int size)
|
||||
{
|
||||
char buf[BUFFLEN];
|
||||
int sent = 0;
|
||||
int buflen = 0;
|
||||
int nbytes;
|
||||
if(size <= BUFFLEN)
|
||||
{
|
||||
nbytes = send(client,data,size,0);
|
||||
return (nbytes==-1?0:1);
|
||||
}
|
||||
else
|
||||
{
|
||||
while(sent < size)
|
||||
{
|
||||
if(size - sent > BUFFLEN)
|
||||
buflen = BUFFLEN;
|
||||
else
|
||||
buflen = size - sent;
|
||||
memcpy(buf,data+sent,buflen);
|
||||
nbytes = send(client,buf,buflen,0);
|
||||
sent += buflen;
|
||||
if(nbytes == -1) return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int __fb(int client, const char* file)
|
||||
{
|
||||
printf("Open file %s\n",file );
|
||||
unsigned char buffer[BUFFLEN];
|
||||
FILE *ptr;
|
||||
ptr = fopen(file,"rb");
|
||||
if(!ptr)
|
||||
{
|
||||
LOG("Cannot read : %s\n", file);
|
||||
return 0;
|
||||
}
|
||||
size_t size;
|
||||
while(!feof(ptr))
|
||||
{
|
||||
size = fread(buffer,1,BUFFLEN,ptr);
|
||||
if(!__b(client,buffer,size)) return 0;
|
||||
}
|
||||
fclose(ptr);
|
||||
return 1;
|
||||
}
|
||||
int __f(int client, const char* file)
|
||||
{
|
||||
unsigned char buf[BUFFLEN];
|
||||
FILE *ptr;
|
||||
int nbytes;
|
||||
ptr = fopen(file,"r");
|
||||
if(!ptr)
|
||||
{
|
||||
LOG("Cannot read : %s\n", file);
|
||||
return 0;
|
||||
}
|
||||
;
|
||||
while(fgets(buf, sizeof(buf), ptr) != NULL)
|
||||
{
|
||||
nbytes = send(client, buf, strlen(buf), 0);
|
||||
if(nbytes == -1) return 0;
|
||||
//LOG("READ : %s\n", buf);
|
||||
//fgets(buf, sizeof(buf), ptr);
|
||||
}
|
||||
fclose(ptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int upload(const char* tmp, const char* path)
|
||||
{
|
||||
return !rename(tmp, path);
|
||||
}
|
||||
// __plugin__.name
|
||||
void set_cookie(int client,const char* type, dictionary dic, const char* name)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: %s",type);
|
||||
association assoc;
|
||||
for_each_assoc(assoc,dic){
|
||||
__t(client,"Set-Cookie: %s=%s; Path=/%s",assoc->key, (char*)assoc->value, name);
|
||||
}
|
||||
response(client,"");
|
||||
}
|
||||
void clear_cookie(int client, dictionary dic)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: text/html; charset=utf-8");
|
||||
association assoc;
|
||||
for_each_assoc(assoc,dic){
|
||||
__t(client,"Set-Cookie: %s=%s;expires=",assoc->key, (char*)assoc->value, server_time());
|
||||
}
|
||||
response(client,"");
|
||||
}
|
||||
void unknow(int client)
|
||||
{
|
||||
html(client);
|
||||
__t(client,"404 API not found");
|
||||
}
|
||||
int ws_enable(dictionary dic)
|
||||
{
|
||||
return (dic != NULL && R_INT(dic,"__web_socket__") == 1);
|
||||
}
|
48
plugins/handle.h
Normal file
48
plugins/handle.h
Normal file
@ -0,0 +1,48 @@
|
||||
#ifndef HANDLE_H
|
||||
#define HANDLE_H
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#ifdef USE_DB
|
||||
#include "dbhelper.h"
|
||||
#endif
|
||||
#include "dictionary.h"
|
||||
#include "list.h"
|
||||
#include "ini.h"
|
||||
#include "ws.h"
|
||||
|
||||
#define SERVER_NAME "antd"
|
||||
#define IS_POST(method) (strcmp(method,"POST")== 0)
|
||||
#define IS_GET(method) (strcmp(method,"GET")== 0)
|
||||
#define R_STR(d,k) ((char*)dvalue(d,k))
|
||||
#define R_INT(d,k) (atoi(dvalue(d,k)))
|
||||
#define R_FLOAT(d,k) ((double)atof(dvalue(d,k)))
|
||||
#define R_PTR(d,k) (dvalue(d,k))
|
||||
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
||||
|
||||
|
||||
|
||||
|
||||
int response(int, const char*);
|
||||
void ctype(int,const char*);
|
||||
void redirect(int,const char*);
|
||||
void html(int);
|
||||
void text(int);
|
||||
void json(int);
|
||||
void jpeg(int);
|
||||
void octstream(int, char*);
|
||||
void textstream(int);
|
||||
int __ti(int,int);
|
||||
int __t(int, const char*,...);
|
||||
int __b(int, const unsigned char*, int);
|
||||
int __f(int, const char*);
|
||||
int __fb(int, const char*);
|
||||
int upload(const char*, const char*);
|
||||
|
||||
void set_cookie(int, const char*,dictionary,const char*);
|
||||
void set_status(int,int,const char*);
|
||||
void clear_cookie(int, dictionary);
|
||||
/*Default function for plugin*/
|
||||
void unknow(int);
|
||||
int ws_enable(dictionary);
|
||||
#endif
|
214
plugins/plugin.c
214
plugins/plugin.c
@ -30,185 +30,6 @@ sqldb getdb()
|
||||
return getdb(__plugin__.name);
|
||||
}
|
||||
#endif
|
||||
void set_status(int client,int code,const char* msg)
|
||||
{
|
||||
response(client, __s("HTTP/1.0 %d %s", code, msg));
|
||||
response(client, __s("Server: %s ", SERVER_NAME));
|
||||
}
|
||||
void redirect(int client,const char*path)
|
||||
{
|
||||
__t(client,"<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"></head><body></body></html>",path);
|
||||
}
|
||||
|
||||
void html(int client)
|
||||
{
|
||||
header(client,"text/html; charset=utf-8");
|
||||
}
|
||||
void text(int client)
|
||||
{
|
||||
header(client,"text/plain; charset=utf-8");
|
||||
}
|
||||
void json(int client)
|
||||
{
|
||||
header(client,"application/json");
|
||||
}
|
||||
void textstream(int client)
|
||||
{
|
||||
header(client, "text/event-stream");
|
||||
}
|
||||
void octstream(int client, char* name)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: application/octet-stream");
|
||||
__t(client,"Content-Disposition: attachment; filename=\"%s\"", name);
|
||||
response(client,"");
|
||||
//Content-Disposition: attachment; filename="fname.ext"
|
||||
}
|
||||
void jpeg(int client)
|
||||
{
|
||||
header(client,"image/jpeg");
|
||||
}
|
||||
void header(int client, const char* type)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: %s",type);
|
||||
response(client,"");
|
||||
}
|
||||
|
||||
int response(int client, const char* data)
|
||||
{
|
||||
char buf[BUFFLEN+3];
|
||||
strcpy(buf, data);
|
||||
int nbytes;
|
||||
int size = strlen(data);
|
||||
buf[size] = '\r';
|
||||
buf[size+1] = '\n';
|
||||
buf[size+2] = '\0';
|
||||
nbytes = send(client, buf, strlen(buf), 0);
|
||||
return (nbytes ==-1?0:1);
|
||||
}
|
||||
int __ti(int client,int data)
|
||||
{
|
||||
char str[15];
|
||||
sprintf(str, "%d", data);
|
||||
return response(client,str);
|
||||
}
|
||||
|
||||
int __t(int client, const char* fstring,...)
|
||||
{
|
||||
int nbytes;
|
||||
int dlen;
|
||||
int sent = 0;
|
||||
int buflen = 0;
|
||||
va_list arguments;
|
||||
char * data;
|
||||
char* chunk;
|
||||
va_start( arguments, fstring);
|
||||
dlen = vsnprintf(0,0,fstring,arguments) + 1;
|
||||
va_end(arguments);
|
||||
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
||||
{
|
||||
va_start(arguments, fstring);
|
||||
vsnprintf(data, dlen, fstring, arguments);
|
||||
va_end(arguments);
|
||||
|
||||
if(dlen < BUFFLEN)
|
||||
return response(client,data);
|
||||
else
|
||||
{
|
||||
while(sent < dlen - 1)
|
||||
{
|
||||
if(dlen - sent > BUFFLEN)
|
||||
buflen = BUFFLEN;
|
||||
else
|
||||
buflen = dlen - sent - 1;
|
||||
//LOG("BUFFLEN %d\n",buflen);
|
||||
chunk = (char*) malloc((buflen)*sizeof(char));
|
||||
memcpy(chunk,data+sent,buflen);
|
||||
//chunk[buflen-1] = '\0';
|
||||
//response(client,chunk);
|
||||
sent += buflen;
|
||||
nbytes = send(client, chunk, buflen, 0);
|
||||
free(chunk);
|
||||
if(nbytes == -1) return 0;
|
||||
}
|
||||
chunk = "\r\n";
|
||||
send(client, chunk, strlen(chunk), 0);
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
return 1;
|
||||
//
|
||||
}
|
||||
int __b(int client, const unsigned char* data, int size)
|
||||
{
|
||||
char buf[BUFFLEN];
|
||||
int sent = 0;
|
||||
int buflen = 0;
|
||||
int nbytes;
|
||||
if(size <= BUFFLEN)
|
||||
{
|
||||
nbytes = send(client,data,size,0);
|
||||
return (nbytes==-1?0:1);
|
||||
}
|
||||
else
|
||||
{
|
||||
while(sent < size)
|
||||
{
|
||||
if(size - sent > BUFFLEN)
|
||||
buflen = BUFFLEN;
|
||||
else
|
||||
buflen = size - sent;
|
||||
memcpy(buf,data+sent,buflen);
|
||||
nbytes = send(client,buf,buflen,0);
|
||||
sent += buflen;
|
||||
if(nbytes == -1) return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int __fb(int client, const char* file)
|
||||
{
|
||||
printf("Open file %s\n",file );
|
||||
unsigned char buffer[BUFFLEN];
|
||||
FILE *ptr;
|
||||
ptr = fopen(file,"rb");
|
||||
if(!ptr)
|
||||
{
|
||||
LOG("Cannot read : %s\n", file);
|
||||
return 0;
|
||||
}
|
||||
size_t size;
|
||||
while(!feof(ptr))
|
||||
{
|
||||
size = fread(buffer,1,BUFFLEN,ptr);
|
||||
if(!__b(client,buffer,size)) return 0;
|
||||
}
|
||||
fclose(ptr);
|
||||
return 1;
|
||||
}
|
||||
int __f(int client, const char* file)
|
||||
{
|
||||
unsigned char buf[BUFFLEN];
|
||||
FILE *ptr;
|
||||
int nbytes;
|
||||
ptr = fopen(file,"r");
|
||||
if(!ptr)
|
||||
{
|
||||
LOG("Cannot read : %s\n", file);
|
||||
return 0;
|
||||
}
|
||||
;
|
||||
while(fgets(buf, sizeof(buf), ptr) != NULL)
|
||||
{
|
||||
nbytes = send(client, buf, strlen(buf), 0);
|
||||
if(nbytes == -1) return 0;
|
||||
//LOG("READ : %s\n", buf);
|
||||
//fgets(buf, sizeof(buf), ptr);
|
||||
}
|
||||
fclose(ptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char* route(const char* repath)
|
||||
{
|
||||
@ -225,6 +46,7 @@ char* route(const char* repath)
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
char* htdocs(const char* repath)
|
||||
{
|
||||
if(repath != NULL)
|
||||
@ -232,30 +54,7 @@ char* htdocs(const char* repath)
|
||||
else
|
||||
return __s("%s",__plugin__.htdocs);
|
||||
}
|
||||
int upload(const char* tmp, const char* path)
|
||||
{
|
||||
return !rename(tmp, path);
|
||||
}
|
||||
void set_cookie(int client,const char* type, dictionary dic)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: %s",type);
|
||||
association assoc;
|
||||
for_each_assoc(assoc,dic){
|
||||
__t(client,"Set-Cookie: %s=%s; Path=/%s",assoc->key, (char*)assoc->value, __plugin__.name);
|
||||
}
|
||||
response(client,"");
|
||||
}
|
||||
void clear_cookie(int client, dictionary dic)
|
||||
{
|
||||
set_status(client,200,"OK");
|
||||
__t(client,"Content-Type: text/html; charset=utf-8");
|
||||
association assoc;
|
||||
for_each_assoc(assoc,dic){
|
||||
__t(client,"Set-Cookie: %s=%s;expires=",assoc->key, (char*)assoc->value, server_time());
|
||||
}
|
||||
response(client,"");
|
||||
}
|
||||
|
||||
char* config_dir()
|
||||
{
|
||||
struct stat st;
|
||||
@ -263,13 +62,4 @@ char* config_dir()
|
||||
if (stat(path, &st) == -1)
|
||||
mkdir(path, 0755);
|
||||
return path;
|
||||
}
|
||||
void unknow(int client)
|
||||
{
|
||||
html(client);
|
||||
__t(client,"404 API not found");
|
||||
}
|
||||
int ws_enable(dictionary dic)
|
||||
{
|
||||
return (dic != NULL && R_INT(dic,"__web_socket__") == 1);
|
||||
}
|
@ -1,29 +1,17 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#ifndef PLUGIN_H
|
||||
#define PLUGIN_H
|
||||
|
||||
#ifdef USE_DB
|
||||
#include "dbhelper.h"
|
||||
#endif
|
||||
#include "dictionary.h"
|
||||
#include "list.h"
|
||||
#include "ini.h"
|
||||
#include "ws.h"
|
||||
|
||||
#define SERVER_NAME "antd"
|
||||
#define IS_POST(method) (strcmp(method,"POST")== 0)
|
||||
#define IS_GET(method) (strcmp(method,"GET")== 0)
|
||||
#define R_STR(d,k) ((char*)dvalue(d,k))
|
||||
#define R_INT(d,k) (atoi(dvalue(d,k)))
|
||||
#define R_FLOAT(d,k) ((double)atof(dvalue(d,k)))
|
||||
#define R_PTR(d,k) (dvalue(d,k))
|
||||
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
||||
#include "handle.h"
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
char *dbpath;
|
||||
char * htdocs;
|
||||
char*pdir;
|
||||
int *sport;
|
||||
int sport;
|
||||
} plugin_header;
|
||||
|
||||
|
||||
@ -35,32 +23,16 @@ typedef sqlite3* sqldb;
|
||||
extern plugin_header __plugin__;
|
||||
extern call __init__;
|
||||
|
||||
int response(int, const char*);
|
||||
void header(int,const char*);
|
||||
void redirect(int,const char*);
|
||||
void html(int);
|
||||
void text(int);
|
||||
void json(int);
|
||||
void jpeg(int);
|
||||
void octstream(int, char*);
|
||||
void textstream(int);
|
||||
int __ti(int,int);
|
||||
int __t(int, const char*,...);
|
||||
int __b(int, const unsigned char*, int);
|
||||
int __f(int, const char*);
|
||||
int __fb(int, const char*);
|
||||
int upload(const char*, const char*);
|
||||
char* config_dir();
|
||||
char* route(const char*);
|
||||
char* htdocs(const char*);
|
||||
|
||||
#ifdef USE_DB
|
||||
sqldb getdb();
|
||||
sqldb __getdb(char *name);
|
||||
#endif
|
||||
void set_cookie(int, const char*,dictionary);
|
||||
void set_status(int,int,const char*);
|
||||
void clear_cookie(int, dictionary);
|
||||
|
||||
char* route(const char*);
|
||||
char* htdocs(const char*);
|
||||
char* config_dir();
|
||||
/*Default function for plugin*/
|
||||
void handler(int, const char*,const char*,dictionary);
|
||||
void unknow(int);
|
||||
int ws_enable(dictionary);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user