Blob


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>
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)"
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);
25 // Generic cache
27 typedef struct Cache Cache;
28 typedef struct CEntry CEntry;
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 };
42 Cache *newcache(int sizeofentry, int maxentry, void (*cefree)(CEntry*));
43 CEntry *cachelookup(Cache*, char*, int);
44 void cacheflush(Cache*, char*);
46 // JSON parser
48 typedef struct Json Json;
50 enum
51 {
52 Jstring,
53 Jnumber,
54 Jobject,
55 Jarray,
56 Jtrue,
57 Jfalse,
58 Jnull
59 };
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 };
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*);
84 // Wrapper to hide whether we're using OpenSSL for HTTPS.
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 };
96 Protocol http;
97 Protocol https;
100 // HTTP library
102 typedef struct HTTPHeader HTTPHeader;
103 struct HTTPHeader
105 int code;
106 char proto[100];
107 char codedesc[100];
108 vlong contentlength;
109 char contenttype[100];
110 };
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);
116 // URL downloader - caches in files on disk
118 int download(char *url, HTTPHeader *hdr);
119 void downloadflush(char*);
121 // JSON RPC
123 enum
125 MaxResponse = 1<<29,
126 };
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*);
132 extern int chattyhttp;
135 // SmugMug RPC
137 #ifdef __GNUC__
138 #define check_nil __attribute__((sentinel))
139 #else
140 #define check_nil
141 #endif
143 Json* smug(char *method, char *name1, ...) check_nil; // cached, http
144 Json* ncsmug(char *method, char *name1, ...) check_nil; // not cached, https
147 // Session information
149 extern Json *userinfo;
150 extern char *sessid;
153 // File system
155 extern Srv xsrv;
156 void xinit(void);
157 extern int nickindex(char*);
159 // Logging
161 typedef struct Logbuf Logbuf;
162 struct Logbuf
164 Req *wait;
165 Req **waitlast;
166 int rp;
167 int wp;
168 char *msg[128];
169 };
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 */
179 extern void rpclog(char*, ...);
180 extern void rpclogflush(Req*);
181 extern void rpclogread(Req*);
182 extern void rpclogwrite(Req*);
184 enum
186 STACKSIZE = 32768
187 };
189 extern int printerrors;