server responds to flist
[reposerve] / server / src / client.c
index bec2f11..e7f4d3c 100644 (file)
@@ -1,12 +1,14 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include <unistd.h>
 #include "client.h"
 #include "proto.h"
 #include "repo.h"
 
 static int proc_request(struct client *c);
+static void print_invalid(const char *s);
 static int proc_flist(struct client *c, char *data, int datasz);
 static int proc_push(struct client *c, char *data, int datasz);
 static int proc_pull(struct client *c, char *data, int datasz);
@@ -28,6 +30,8 @@ int handle_client(struct client *c)
        if(proc_request(c) == -1) {
                return -1;
        }
+
+       if(sz == 0) return -1;
        return 0;
 }
 
@@ -38,7 +42,12 @@ static int proc_request(struct client *c)
        int reqdata_len;
 
        while(endp < c->buf + c->bsz) {
+               if(*endp == '\r' && endp[1] == '\n') {
+                       *endp++ = 0;
+                       break;
+               }
                if(*endp == '\n') break;
+               endp++;
        }
        if(endp >= c->buf + c->bsz) {
                return 0;
@@ -62,10 +71,27 @@ static int proc_request(struct client *c)
                return 0;
        }
 
+       print_invalid(c->buf);
        send_err(c);
        return 0;
 }
 
+static void print_invalid(const char *s)
+{
+       int i;
+       printf("Recv invalid request: \"");
+       for(i=0; i<60; i++) {
+               if(!*s) break;
+               if(isprint(*s)) {
+                       putchar(*s);
+               } else {
+                       printf("\\x%x", (unsigned int)*s);
+               }
+               s++;
+       }
+       puts("\"");
+}
+
 void send_string(struct client *c, const char *s)
 {
        write(c->s, s, strlen(s));
@@ -85,8 +111,10 @@ void send_err(struct client *c)
 
 static struct flist *gen_flist(int contents)
 {
-       int i, count;
+       int i, count, len;
        struct flist *flist;
+       char *pathbuf = 0;
+       int pathbuf_sz = 0;
 
        if(!(flist = flist_create())) {
                return 0;
@@ -98,10 +126,22 @@ static struct flist *gen_flist(int contents)
        count = repo_num_files();
 
        for(i=0; i<count; i++) {
-               flist_add(flist, repo_file(i)->path, contents);
+               len = strlen(repo_path) + strlen(repo_file(i)->path) + 1;
+               if(len > pathbuf_sz) {
+                       free(pathbuf);
+                       if(!(pathbuf = malloc(len + 1))) {
+                               fprintf(stderr, "gen_flist: failed to allocate path buffer (%d bytes)\n", len + 1);
+                               repo_cleanup();
+                               flist_destroy(flist);
+                               return 0;
+                       }
+               }
+               sprintf(pathbuf, "%s/%s", repo_path, repo_file(i)->path);
+               flist_add(flist, pathbuf, contents);
        }
        repo_cleanup();
 
+       free(pathbuf);
        return flist;
 }