4 // Copyright (c) 1996-2021 by the citadel.org team
6 // This program is open source software. It runs great on the
7 // Linux operating system (and probably elsewhere). You can use,
8 // copy, and run it under the terms of the GNU General Public
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
19 // Fetch a user photo (avatar)
20 void fetch_user_photo(struct http_transaction *h, struct ctdlsession *c, char *username) {
22 int content_length = 0;
23 char content_type[1024];
25 int actual_length = 0;
27 ctdl_printf(c, "DLUI %s", username);
28 ctdl_readline(c, buf, sizeof(buf));
30 content_length = extract_int(&buf[4], 0);
31 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
33 image = malloc(content_length);
38 actual_length = ctdl_read_binary(c, image, content_length);
40 add_response_header(h, strdup("Content-type"), strdup(content_type));
41 h->response_code = 200;
42 h->response_string = strdup("OK");
43 h->response_body_length = actual_length;
44 h->response_body = image;
52 // Fetch a user bio (profile)
53 void fetch_user_bio(struct http_transaction *h, struct ctdlsession *c, char *username) {
54 do_404(h); // FIXME finish this
58 // Client requested an object related to a user.
59 void object_in_user(struct http_transaction *h, struct ctdlsession *c, char *requested_username) {
60 char object_name[1024];
62 extract_token(object_name, h->url, 4, '/', sizeof object_name);
64 if (!strcasecmp(object_name, "userpic")) { // user photo (avatar)
65 fetch_user_photo(h, c, requested_username);
69 if (!strcasecmp(object_name, "bio")) { // user bio (profile)
70 fetch_user_bio(h, c, requested_username);
74 do_404(h); // unknown object
79 // Handle REST/DAV requests for the user itself (such as /ctdl/u/username
80 // or /ctdl/i/username/ but *not* specific properties of the user)
81 void the_user_itself(struct http_transaction *h, struct ctdlsession *c, char *username) {
86 // Dispatcher for "/ctdl/u" and "/ctdl/u/" for the user list
87 void user_list(struct http_transaction *h, struct ctdlsession *c) {
92 // Dispatcher for paths starting with /ctdl/u/
93 void ctdl_u(struct http_transaction *h, struct ctdlsession *c) {
94 char requested_username[128];
97 // All user-related functions require accessing the user in question.
98 extract_token(requested_username, h->url, 3, '/', sizeof requested_username);
99 unescape_input(requested_username);
101 if (IsEmptyStr(requested_username)) { // /ctdl/u/
106 // At this point we have extracted the name of the user we're interested in.
107 // FIXME should we validate it?
109 if (num_tokens(h->url, '/') == 4) { // /ctdl/u/username
110 the_user_itself(h, c, requested_username);
114 extract_token(buf, h->url, 4, '/', sizeof buf);
115 if (num_tokens(h->url, '/') == 5) {
116 if (IsEmptyStr(buf)) {
117 the_user_itself(h, c, requested_username); // /ctdl/u/username/ ( same as /ctdl/u/username )
120 object_in_user(h, c, requested_username); // /ctdl/u/username/object
125 if (num_tokens(h->url, '/') == 6) {
126 object_in_user(h, c, requested_username); // /ctdl/u/username/object/ or possibly /ctdl/u/username/object/component
130 // If we get to this point, the client requested an action we don't know how to perform.