10 static int proc_request(struct client *c);
11 static void print_invalid(const char *s);
12 static int proc_flist(struct client *c, char *data, int datasz);
13 static int proc_push(struct client *c, char *data, int datasz);
14 static int proc_pull(struct client *c, char *data, int datasz);
16 const char *repo_path;
18 int handle_client(struct client *c)
23 while((sz = read(c->s, buf, sizeof buf)) > 0) {
24 left = sizeof c->buf - c->bsz;
25 if(sz > left) sz = left;
26 memcpy(c->buf + c->bsz, buf, sz);
30 if(proc_request(c) == -1) {
34 if(sz == 0) return -1;
38 static int proc_request(struct client *c)
44 while(endp < c->buf + c->bsz) {
45 if(*endp == '\r' && endp[1] == '\n') {
49 if(*endp == '\n') break;
52 if(endp >= c->buf + c->bsz) {
58 reqdata_len = c->bsz - (reqdata - c->buf);
62 if(strcmp(c->buf, "flist") == 0) {
63 return proc_flist(c, reqdata, reqdata_len);
64 } else if(strcmp(c->buf, "push") == 0) {
65 return proc_push(c, reqdata, reqdata_len);
66 } else if(strcmp(c->buf, "pull") == 0) {
67 return proc_pull(c, reqdata, reqdata_len);
68 } else if(strcmp(c->buf, "dos") == 0) {
69 c->flags |= CLIENT_DOS;
74 print_invalid(c->buf);
79 static void print_invalid(const char *s)
82 printf("Recv invalid request: \"");
88 printf("\\x%x", (unsigned int)*s);
95 void send_string(struct client *c, const char *s)
97 write(c->s, s, strlen(s));
100 void send_ok(struct client *c, int resp_sz)
103 sprintf(buf, "OK %d\n", resp_sz < 0 ? 0 : resp_sz);
107 void send_err(struct client *c)
109 send_string(c, "ERR\n");
112 static struct flist *gen_flist(int contents)
119 if(!(flist = flist_create())) {
123 if(repo_init(repo_path) == -1) {
126 count = repo_num_files();
128 for(i=0; i<count; i++) {
129 len = strlen(repo_path) + strlen(repo_file(i)->path) + 1;
130 if(len > pathbuf_sz) {
132 if(!(pathbuf = malloc(len + 1))) {
133 fprintf(stderr, "gen_flist: failed to allocate path buffer (%d bytes)\n", len + 1);
135 flist_destroy(flist);
139 sprintf(pathbuf, "%s/%s", repo_path, repo_file(i)->path);
140 flist_add(flist, pathbuf, contents);
148 static int proc_flist(struct client *c, char *data, int datasz)
152 if(!(flist = gen_flist(0))) {
156 flist_finalize(flist);
158 send_ok(c, flist->final_size);
159 write(c->s, flist->flist, flist->final_size);
161 flist_destroy(flist);
165 static int proc_push(struct client *c, char *data, int datasz)
171 static int proc_pull(struct client *c, char *data, int datasz)