URI:
       ta.h - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       ta.h (3425B)
       ---
            1 #include <u.h>
            2 #include <errno.h>
            3 #include <libc.h>
            4 #include <fcall.h>
            5 #include <thread.h>
            6 #include <auth.h>
            7 #include <9p.h>
            8 #include <libsec.h>
            9 
           10 #define APIKEY        "G9ANE2zvCozKEoLQ5qaR1AUtcE5YpuDj"
           11 #define HOST          "api.smugmug.com"
           12 #define UPLOAD_HOST   "upload.smugmug.com"
           13 #define API_VERSION   "1.2.1"
           14 #define PATH          "/services/api/json/" API_VERSION "/"
           15 #define USER_AGENT    "smugfs (part of Plan 9 from User Space)"
           16 
           17 void*        emalloc(int);
           18 void*        erealloc(void*, int);
           19 char*        estrdup(char*);
           20 int        urlencodefmt(Fmt*);
           21 int        timefmt(Fmt*);
           22 int        writen(int, void*, int);
           23 
           24 
           25 // Generic cache
           26 
           27 typedef struct Cache Cache;
           28 typedef struct CEntry CEntry;
           29 
           30 struct CEntry
           31 {
           32         char *name;
           33         struct {
           34                 CEntry *next;
           35                 CEntry *prev;
           36         } list;
           37         struct {
           38                 CEntry *next;
           39         } hash;
           40 };
           41 
           42 Cache *newcache(int sizeofentry, int maxentry, void (*cefree)(CEntry*));
           43 CEntry *cachelookup(Cache*, char*, int);
           44 void cacheflush(Cache*, char*);
           45 
           46 // JSON parser
           47 
           48 typedef struct Json Json;
           49 
           50 enum
           51 {
           52         Jstring,
           53         Jnumber,
           54         Jobject,
           55         Jarray,
           56         Jtrue,
           57         Jfalse,
           58         Jnull
           59 };
           60 
           61 struct Json
           62 {
           63         int ref;
           64         int type;
           65         char *string;
           66         double number;
           67         char **name;
           68         Json **value;
           69         int len;
           70 };
           71 
           72 void        jclose(Json*);
           73 Json*        jincref(Json*);
           74 vlong        jint(Json*);
           75 Json*        jlookup(Json*, char*);
           76 double        jnumber(Json*);
           77 int        jsonfmt(Fmt*);
           78 int        jstrcmp(Json*, char*);
           79 char*        jstring(Json*);
           80 Json*        jwalk(Json*, char*);
           81 Json*        parsejson(char*);
           82 
           83 
           84 // Wrapper to hide whether we're using OpenSSL for HTTPS.
           85 
           86 typedef struct Protocol Protocol;
           87 typedef struct Pfd Pfd;
           88 struct Protocol
           89 {
           90         Pfd *(*connect)(char *host);
           91         int (*read)(Pfd*, void*, int);
           92         int (*write)(Pfd*, void*, int);
           93         void (*close)(Pfd*);
           94 };
           95 
           96 Protocol http;
           97 Protocol https;
           98 
           99 
          100 // HTTP library
          101 
          102 typedef struct HTTPHeader HTTPHeader;
          103 struct HTTPHeader
          104 {
          105         int code;
          106         char proto[100];
          107         char codedesc[100];
          108         vlong contentlength;
          109         char contenttype[100];
          110 };
          111 
          112 char *httpreq(Protocol *proto, char *host, char *request, HTTPHeader *hdr, int rfd, vlong rlength);
          113 int httptofile(Protocol *proto, char *host, char *req, HTTPHeader *hdr, int wfd);
          114 
          115 
          116 // URL downloader - caches in files on disk
          117 
          118 int download(char *url, HTTPHeader *hdr);
          119 void downloadflush(char*);
          120 
          121 // JSON RPC
          122 
          123 enum
          124 {
          125         MaxResponse = 1<<29,
          126 };
          127 
          128 Json*        jsonrpc(Protocol *proto, char *host, char *path, char *method, char *name1, va_list arg, int usecache);
          129 Json*        jsonupload(Protocol *proto, char *host, char *req, int rfd, vlong rlength);
          130 void        jcacheflush(char*);
          131 
          132 extern int chattyhttp;
          133 
          134 
          135 // SmugMug RPC
          136 
          137 #ifdef __GNUC__
          138 #define check_nil __attribute__((sentinel))
          139 #else
          140 #define check_nil
          141 #endif
          142 
          143 Json* smug(char *method, char *name1, ...) check_nil;  // cached, http
          144 Json* ncsmug(char *method, char *name1, ...) check_nil;  // not cached, https
          145 
          146 
          147 // Session information
          148 
          149 extern Json *userinfo;
          150 extern char *sessid;
          151 
          152 
          153 // File system
          154 
          155 extern Srv xsrv;
          156 void xinit(void);
          157 extern int nickindex(char*);
          158 
          159 // Logging
          160 
          161 typedef struct Logbuf Logbuf;
          162 struct Logbuf
          163 {
          164         Req *wait;
          165         Req **waitlast;
          166         int rp;
          167         int wp;
          168         char *msg[128];
          169 };
          170 
          171 extern void        lbkick(Logbuf*);
          172 extern void        lbappend(Logbuf*, char*, ...);
          173 extern void        lbvappend(Logbuf*, char*, va_list);
          174 /* #pragma varargck argpos lbappend 2 */
          175 extern void        lbread(Logbuf*, Req*);
          176 extern void        lbflush(Logbuf*, Req*);
          177 /* #pragma varargck argpos flog 1 */
          178 
          179 extern void        rpclog(char*, ...);
          180 extern void        rpclogflush(Req*);
          181 extern void        rpclogread(Req*);
          182 extern void        rpclogwrite(Req*);
          183 
          184 enum
          185 {
          186         STACKSIZE = 32768
          187 };
          188 
          189 extern int printerrors;