2 * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
20 #include <sys/types.h>
34 #include <got_object.h>
35 #include <got_reference.h>
36 #include <got_repository.h>
38 #include <got_cancel.h>
39 #include <got_worktree.h>
41 #include <got_commit_graph.h>
42 #include <got_blame.h>
43 #include <got_privsep.h>
44 #include <got_opentemp.h>
51 #include "gotweb_ui.h"
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 TAILQ_HEAD(headers, gw_header) gw_headers;
59 TAILQ_HEAD(dirs, gw_dir) gw_dirs;
60 struct gw_dir *gw_dir;
61 struct gotweb_conf *gw_conf;
62 struct ktemplate *gw_tmpl;
63 struct khtmlreq *gw_html_req;
74 unsigned int repos_total;
79 TAILQ_ENTRY(gw_header) entry;
80 struct got_repository *repo;
81 struct got_reflist_head refs;
82 struct got_commit_object *commit;
83 struct got_object_id *id;
87 char *commit_id; /* id_str1 */
88 char *parent_id; /* id_str2 */
93 time_t committer_time;
97 TAILQ_ENTRY(gw_dir) entry;
138 static const char *const gw_templs[TEMPL__MAX] = {
148 static const struct kvalid gw_keys[KEY__ZMAX] = {
149 { kvalid_stringne, "action" },
150 { kvalid_stringne, "commit" },
151 { kvalid_stringne, "file" },
152 { kvalid_stringne, "folder" },
153 { kvalid_stringne, "headref" },
154 { kvalid_int, "page" },
155 { kvalid_stringne, "path" },
158 static struct gw_dir *gw_init_gw_dir(char *);
159 static struct gw_header *gw_init_header(void);
161 static char *gw_get_repo_description(struct gw_trans *,
163 static char *gw_get_repo_owner(struct gw_trans *,
165 static char *gw_get_time_str(time_t, int);
166 static char *gw_get_repo_age(struct gw_trans *,
167 char *, char *, int);
168 static char *gw_get_file_blame(struct gw_trans *);
169 static char *gw_get_repo_tree(struct gw_trans *);
170 static char *gw_get_diff(struct gw_trans *,
172 static char *gw_get_repo_tags(struct gw_trans *,
173 struct gw_header *, int, int);
174 static char *gw_get_repo_heads(struct gw_trans *);
175 static char *gw_get_clone_url(struct gw_trans *, char *);
176 static char *gw_get_got_link(struct gw_trans *);
177 static char *gw_get_site_link(struct gw_trans *);
178 static char *gw_html_escape(const char *);
179 static char *gw_colordiff_line(char *);
181 static char *gw_gen_commit_header(char *, char*);
182 static char *gw_gen_diff_header(char *, char*);
183 static char *gw_gen_author_header(char *);
184 static char *gw_gen_committer_header(char *);
185 static char *gw_gen_age_header(char *);
186 static char *gw_gen_commit_msg_header(char *);
187 static char *gw_gen_tree_header(char *);
189 static void gw_free_headers(struct gw_header *);
190 static const struct got_error* gw_display_open(struct gw_trans *, enum khttp,
192 static const struct got_error* gw_display_index(struct gw_trans *,
193 const struct got_error *);
195 static int gw_template(size_t, void *);
197 static const struct got_error* gw_get_header(struct gw_trans *,
198 struct gw_header *, int);
199 static const struct got_error* gw_get_commits(struct gw_trans *,
200 struct gw_header *, int);
201 static const struct got_error* gw_get_commit(struct gw_trans *,
203 static const struct got_error* gw_apply_unveil(const char *, const char *);
204 static const struct got_error* gw_blame_cb(void *, int, int,
205 struct got_object_id *);
206 static const struct got_error* gw_load_got_paths(struct gw_trans *);
207 static const struct got_error* gw_load_got_path(struct gw_trans *,
209 static const struct got_error* gw_parse_querystring(struct gw_trans *);
211 static const struct got_error* gw_blame(struct gw_trans *);
212 static const struct got_error* gw_diff(struct gw_trans *);
213 static const struct got_error* gw_index(struct gw_trans *);
214 static const struct got_error* gw_commits(struct gw_trans *);
215 static const struct got_error* gw_briefs(struct gw_trans *);
216 static const struct got_error* gw_summary(struct gw_trans *);
217 static const struct got_error* gw_tree(struct gw_trans *);
218 static const struct got_error* gw_tag(struct gw_trans *);
220 struct gw_query_action {
221 unsigned int func_id;
222 const char *func_name;
223 const struct got_error *(*func_main)(struct gw_trans *);
227 enum gw_query_actions {
239 static struct gw_query_action gw_query_funcs[] = {
240 { GW_BLAME, "blame", gw_blame, "gw_tmpl/blame.tmpl" },
241 { GW_BRIEFS, "briefs", gw_briefs, "gw_tmpl/briefs.tmpl" },
242 { GW_COMMITS, "commits", gw_commits, "gw_tmpl/commit.tmpl" },
243 { GW_DIFF, "diff", gw_diff, "gw_tmpl/diff.tmpl" },
244 { GW_ERR, NULL, NULL, "gw_tmpl/err.tmpl" },
245 { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
246 { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/summry.tmpl" },
247 { GW_TAG, "tag", gw_tag, "gw_tmpl/tag.tmpl" },
248 { GW_TREE, "tree", gw_tree, "gw_tmpl/tree.tmpl" },
251 static const struct got_error *
252 gw_kcgi_error(enum kcgi_err kerr)
257 if (kerr == KCGI_EXIT || kerr == KCGI_HUP)
258 return got_error(GOT_ERR_CANCELLED);
260 if (kerr == KCGI_ENOMEM)
261 return got_error_set_errno(ENOMEM, kcgi_strerror(kerr));
263 if (kerr == KCGI_ENFILE)
264 return got_error_set_errno(ENFILE, kcgi_strerror(kerr));
266 if (kerr == KCGI_EAGAIN)
267 return got_error_set_errno(EAGAIN, kcgi_strerror(kerr));
269 if (kerr == KCGI_FORM)
270 return got_error_msg(GOT_ERR_IO, kcgi_strerror(kerr));
272 return got_error_from_errno(kcgi_strerror(kerr));
275 static const struct got_error *
276 gw_apply_unveil(const char *repo_path, const char *repo_file)
278 const struct got_error *err;
280 if (repo_path && repo_file) {
282 if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
283 return got_error_from_errno("asprintf unveil");
284 if (unveil(full_path, "r") != 0)
285 return got_error_from_errno2("unveil", full_path);
288 if (repo_path && unveil(repo_path, "r") != 0)
289 return got_error_from_errno2("unveil", repo_path);
291 if (unveil("/tmp", "rwc") != 0)
292 return got_error_from_errno2("unveil", "/tmp");
294 err = got_privsep_unveil_exec_helpers();
298 if (unveil(NULL, NULL) != 0)
299 return got_error_from_errno("unveil");
304 static const struct got_error *
305 gw_blame(struct gw_trans *gw_trans)
307 const struct got_error *error = NULL;
308 struct gw_header *header = NULL;
309 char *blame = NULL, *blame_html = NULL, *blame_html_disp = NULL;
312 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
314 return got_error_from_errno("pledge");
316 if ((header = gw_init_header()) == NULL)
317 return got_error_from_errno("malloc");
319 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
323 error = gw_get_header(gw_trans, header, 1);
327 blame_html = gw_get_file_blame(gw_trans);
329 if (blame_html == NULL) {
330 blame_html = strdup("");
331 if (blame_html == NULL)
332 return got_error_from_errno("strdup");
335 if ((asprintf(&blame_html_disp, blame_header,
336 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
337 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
338 blame_html)) == -1) {
339 error = got_error_from_errno("asprintf");
343 if (asprintf(&blame, blame_wrapper, blame_html_disp) == -1) {
344 error = got_error_from_errno("asprintf");
348 kerr = khttp_puts(gw_trans->gw_req, blame);
350 error = gw_kcgi_error(kerr);
352 got_ref_list_free(&header->refs);
353 gw_free_headers(header);
354 free(blame_html_disp);
360 static const struct got_error *
361 gw_diff(struct gw_trans *gw_trans)
363 const struct got_error *error = NULL;
364 struct gw_header *header = NULL;
365 char *diff = NULL, *diff_html = NULL, *diff_html_disp = NULL;
368 if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
370 return got_error_from_errno("pledge");
372 if ((header = gw_init_header()) == NULL)
373 return got_error_from_errno("malloc");
375 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
379 error = gw_get_header(gw_trans, header, 1);
383 diff_html = gw_get_diff(gw_trans, header);
385 if (diff_html == NULL) {
386 diff_html = strdup("");
387 if (diff_html == NULL) {
388 error = got_error_from_errno("strdup");
393 if (asprintf(&diff_html_disp, diff_header,
394 gw_gen_diff_header(header->parent_id, header->commit_id),
395 gw_gen_commit_header(header->commit_id, header->refs_str),
396 gw_gen_tree_header(header->tree_id),
397 gw_gen_author_header(header->author),
398 gw_gen_committer_header(header->committer),
399 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
400 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
402 error = got_error_from_errno("asprintf");
406 if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
407 error = got_error_from_errno("asprintf");
411 kerr = khttp_puts(gw_trans->gw_req, diff);
413 error = gw_kcgi_error(kerr);
415 got_ref_list_free(&header->refs);
416 gw_free_headers(header);
417 free(diff_html_disp);
423 static const struct got_error *
424 gw_index(struct gw_trans *gw_trans)
426 const struct got_error *error = NULL;
427 struct gw_dir *gw_dir = NULL;
428 char *html, *navs, *next, *prev;
429 unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
432 if (pledge("stdio rpath proc exec sendfd unveil",
434 error = got_error_from_errno("pledge");
438 error = gw_apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
442 error = gw_load_got_paths(gw_trans);
446 kerr = khttp_puts(gw_trans->gw_req, index_projects_header);
448 return gw_kcgi_error(kerr);
450 if (TAILQ_EMPTY(&gw_trans->gw_dirs)) {
451 if (asprintf(&html, index_projects_empty,
452 gw_trans->gw_conf->got_repos_path) == -1)
453 return got_error_from_errno("asprintf");
454 kerr = khttp_puts(gw_trans->gw_req, html);
456 error = gw_kcgi_error(kerr);
461 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
464 TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
465 if (gw_trans->page > 0 && (gw_trans->page *
466 gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
475 if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
476 gw_dir->name, gw_dir->name)) == -1)
477 return got_error_from_errno("asprintf");
479 if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
480 gw_dir->description, gw_dir->owner, gw_dir->age,
482 return got_error_from_errno("asprintf");
484 kerr = khttp_puts(gw_trans->gw_req, html);
488 return gw_kcgi_error(kerr);
490 if (gw_trans->gw_conf->got_max_repos_display == 0)
493 if (next_disp == gw_trans->gw_conf->got_max_repos_display) {
494 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
496 return gw_kcgi_error(kerr);
497 } else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
498 (gw_trans->page > 0) &&
499 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
500 prev_disp == gw_trans->repos_total)) {
501 kerr = khttp_puts(gw_trans->gw_req, np_wrapper_start);
503 return gw_kcgi_error(kerr);
506 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
507 (gw_trans->page > 0) &&
508 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
509 prev_disp == gw_trans->repos_total)) {
510 if ((asprintf(&prev, nav_prev,
511 gw_trans->page - 1)) == -1)
512 return got_error_from_errno("asprintf");
513 kerr = khttp_puts(gw_trans->gw_req, prev);
516 return gw_kcgi_error(kerr);
519 kerr = khttp_puts(gw_trans->gw_req, div_end);
521 return gw_kcgi_error(kerr);
523 if (gw_trans->gw_conf->got_max_repos_display > 0 &&
524 next_disp == gw_trans->gw_conf->got_max_repos_display &&
525 dir_c != (gw_trans->page + 1) *
526 gw_trans->gw_conf->got_max_repos_display) {
527 if ((asprintf(&next, nav_next,
528 gw_trans->page + 1)) == -1)
529 return got_error_from_errno("calloc");
530 kerr = khttp_puts(gw_trans->gw_req, next);
533 return gw_kcgi_error(kerr);
534 kerr = khttp_puts(gw_trans->gw_req, div_end);
536 error = gw_kcgi_error(kerr);
541 if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
542 (gw_trans->page > 0) &&
543 (next_disp == gw_trans->gw_conf->got_max_repos_display ||
544 prev_disp == gw_trans->repos_total)) {
545 kerr = khttp_puts(gw_trans->gw_req, div_end);
547 return gw_kcgi_error(kerr);
555 static const struct got_error *
556 gw_commits(struct gw_trans *gw_trans)
558 const struct got_error *error = NULL;
559 char *commits_html, *commits_navs_html;
560 struct gw_header *header = NULL, *n_header = NULL;
563 if ((header = gw_init_header()) == NULL)
564 return got_error_from_errno("malloc");
566 if (pledge("stdio rpath proc exec sendfd unveil",
568 error = got_error_from_errno("pledge");
572 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
576 error = gw_get_header(gw_trans, header,
577 gw_trans->gw_conf->got_max_commits_display);
579 kerr = khttp_puts(gw_trans->gw_req, commits_wrapper);
581 return gw_kcgi_error(kerr);
582 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
583 if ((asprintf(&commits_navs_html, commits_navs,
584 gw_trans->repo_name, n_header->commit_id,
585 gw_trans->repo_name, n_header->commit_id,
586 gw_trans->repo_name, n_header->commit_id)) == -1)
587 return got_error_from_errno("asprintf");
589 if ((asprintf(&commits_html, commits_line,
590 gw_gen_commit_header(n_header->commit_id,
592 gw_gen_author_header(n_header->author),
593 gw_gen_committer_header(n_header->committer),
594 gw_gen_age_header(gw_get_time_str(n_header->committer_time,
595 TM_LONG)), gw_html_escape(n_header->commit_msg),
596 commits_navs_html)) == -1)
597 return got_error_from_errno("asprintf");
598 kerr = khttp_puts(gw_trans->gw_req, commits_html);
600 return gw_kcgi_error(kerr);
602 kerr = khttp_puts(gw_trans->gw_req, div_end);
604 error = gw_kcgi_error(kerr);
606 got_ref_list_free(&header->refs);
607 gw_free_headers(header);
608 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
609 gw_free_headers(n_header);
613 static const struct got_error *
614 gw_briefs(struct gw_trans *gw_trans)
616 const struct got_error *error = NULL;
617 char *briefs_html = NULL, *briefs_navs_html = NULL, *newline;
618 struct gw_header *header = NULL, *n_header = NULL;
621 if ((header = gw_init_header()) == NULL)
622 return got_error_from_errno("malloc");
624 if (pledge("stdio rpath proc exec sendfd unveil",
626 error = got_error_from_errno("pledge");
630 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
634 if (gw_trans->action == GW_SUMMARY)
635 error = gw_get_header(gw_trans, header, D_MAXSLCOMMDISP);
637 error = gw_get_header(gw_trans, header,
638 gw_trans->gw_conf->got_max_commits_display);
643 kerr = khttp_puts(gw_trans->gw_req, briefs_wrapper);
645 return gw_kcgi_error(kerr);
647 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry) {
648 if ((asprintf(&briefs_navs_html, briefs_navs,
649 gw_trans->repo_name, n_header->commit_id,
650 gw_trans->repo_name, n_header->commit_id,
651 gw_trans->repo_name, n_header->commit_id)) == -1)
652 return got_error_from_errno("asprintf");
653 newline = strchr(n_header->commit_msg, '\n');
656 if ((asprintf(&briefs_html, briefs_line,
657 gw_get_time_str(n_header->committer_time, TM_DIFF),
658 n_header->author, gw_html_escape(n_header->commit_msg),
659 briefs_navs_html)) == -1)
660 return got_error_from_errno("asprintf");
661 kerr = khttp_puts(gw_trans->gw_req, briefs_html);
663 return gw_kcgi_error(kerr);
665 kerr = khttp_puts(gw_trans->gw_req, div_end);
667 error = gw_kcgi_error(kerr);
669 got_ref_list_free(&header->refs);
670 gw_free_headers(header);
671 TAILQ_FOREACH(n_header, &gw_trans->gw_headers, entry)
672 gw_free_headers(n_header);
676 static const struct got_error *
677 gw_summary(struct gw_trans *gw_trans)
679 const struct got_error *error = NULL;
680 char *description_html, *repo_owner_html, *repo_age_html,
681 *cloneurl_html, *tags, *heads, *tags_html,
685 if (pledge("stdio rpath proc exec sendfd unveil",
687 error = got_error_from_errno("pledge");
691 /* unveil is applied with gw_briefs below */
693 kerr = khttp_puts(gw_trans->gw_req, summary_wrapper);
695 return gw_kcgi_error(kerr);
697 if (gw_trans->gw_conf->got_show_repo_description) {
698 if (gw_trans->gw_dir->description != NULL &&
699 (strcmp(gw_trans->gw_dir->description, "") != 0)) {
700 if ((asprintf(&description_html, description,
701 gw_trans->gw_dir->description)) == -1)
702 return got_error_from_errno("asprintf");
704 kerr = khttp_puts(gw_trans->gw_req, description_html);
705 free(description_html);
707 return gw_kcgi_error(kerr);
711 if (gw_trans->gw_conf->got_show_repo_owner) {
712 if (gw_trans->gw_dir->owner != NULL &&
713 (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
714 if ((asprintf(&repo_owner_html, repo_owner,
715 gw_trans->gw_dir->owner)) == -1)
716 return got_error_from_errno("asprintf");
718 kerr = khttp_puts(gw_trans->gw_req, repo_owner_html);
719 free(repo_owner_html);
721 return gw_kcgi_error(kerr);
725 if (gw_trans->gw_conf->got_show_repo_age) {
726 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
727 "refs/heads", TM_LONG);
728 if (age != NULL && (strcmp(age, "") != 0)) {
729 if ((asprintf(&repo_age_html, last_change, age)) == -1)
730 return got_error_from_errno("asprintf");
732 kerr = khttp_puts(gw_trans->gw_req, repo_age_html);
736 return gw_kcgi_error(kerr);
740 if (gw_trans->gw_conf->got_show_repo_cloneurl) {
741 if (gw_trans->gw_dir->url != NULL &&
742 (strcmp(gw_trans->gw_dir->url, "") != 0)) {
743 if ((asprintf(&cloneurl_html, cloneurl,
744 gw_trans->gw_dir->url)) == -1)
745 return got_error_from_errno("asprintf");
747 kerr = khttp_puts(gw_trans->gw_req, cloneurl_html);
750 return gw_kcgi_error(kerr);
753 kerr = khttp_puts(gw_trans->gw_req, div_end);
755 return gw_kcgi_error(kerr);
757 error = gw_briefs(gw_trans);
761 tags = gw_get_repo_tags(gw_trans, NULL, D_MAXSLCOMMDISP, TAGBRIEF);
762 heads = gw_get_repo_heads(gw_trans);
764 if (tags != NULL && strcmp(tags, "") != 0) {
765 if ((asprintf(&tags_html, summary_tags,
767 return got_error_from_errno("asprintf");
768 kerr = khttp_puts(gw_trans->gw_req, tags_html);
772 return gw_kcgi_error(kerr);
775 if (heads != NULL && strcmp(heads, "") != 0) {
776 if ((asprintf(&heads_html, summary_heads,
778 return got_error_from_errno("asprintf");
779 kerr = khttp_puts(gw_trans->gw_req, heads_html);
783 return gw_kcgi_error(kerr);
788 static const struct got_error *
789 gw_tree(struct gw_trans *gw_trans)
791 const struct got_error *error = NULL;
792 struct gw_header *header = NULL;
793 char *tree = NULL, *tree_html = NULL, *tree_html_disp = NULL;
796 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
797 return got_error_from_errno("pledge");
799 if ((header = gw_init_header()) == NULL)
800 return got_error_from_errno("malloc");
802 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
806 error = gw_get_header(gw_trans, header, 1);
810 tree_html = gw_get_repo_tree(gw_trans);
812 if (tree_html == NULL) {
813 tree_html = strdup("");
814 if (tree_html == NULL) {
815 error = got_error_from_errno("strdup");
820 if ((asprintf(&tree_html_disp, tree_header,
821 gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
822 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
824 return got_error_from_errno("asprintf");
826 if ((asprintf(&tree, tree_wrapper, tree_html_disp)) == -1)
827 return got_error_from_errno("asprintf");
829 kerr = khttp_puts(gw_trans->gw_req, tree);
831 error = gw_kcgi_error(kerr);
833 got_ref_list_free(&header->refs);
834 gw_free_headers(header);
835 free(tree_html_disp);
841 static const struct got_error *
842 gw_tag(struct gw_trans *gw_trans)
844 const struct got_error *error = NULL;
845 struct gw_header *header = NULL;
846 char *tag = NULL, *tag_html = NULL, *tag_html_disp = NULL;
849 if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1)
850 return got_error_from_errno("pledge");
852 if ((header = gw_init_header()) == NULL)
853 return got_error_from_errno("malloc");
855 error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
859 error = gw_get_header(gw_trans, header, 1);
863 tag_html = gw_get_repo_tags(gw_trans, header, 1, TAGFULL);
865 if (tag_html == NULL) {
866 tag_html = strdup("");
867 if (tag_html == NULL) {
868 error = got_error_from_errno("strdup");
873 if ((asprintf(&tag_html_disp, tag_header,
874 gw_gen_commit_header(header->commit_id, header->refs_str),
875 gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
877 return got_error_from_errno("asprintf");
879 if ((asprintf(&tag, tag_wrapper, tag_html_disp)) == -1)
880 return got_error_from_errno("asprintf");
882 kerr = khttp_puts(gw_trans->gw_req, tag);
884 error = gw_kcgi_error(kerr);
886 got_ref_list_free(&header->refs);
887 gw_free_headers(header);
894 static const struct got_error *
895 gw_load_got_path(struct gw_trans *gw_trans, struct gw_dir *gw_dir)
897 const struct got_error *error = NULL;
902 if ((asprintf(&dir_test, "%s/%s/%s",
903 gw_trans->gw_conf->got_repos_path, gw_dir->name,
904 GOTWEB_GIT_DIR)) == -1)
905 return got_error_from_errno("asprintf");
907 dt = opendir(dir_test);
911 gw_dir->path = strdup(dir_test);
916 if ((asprintf(&dir_test, "%s/%s/%s",
917 gw_trans->gw_conf->got_repos_path, gw_dir->name,
918 GOTWEB_GOT_DIR)) == -1)
919 return got_error_from_errno("asprintf");
921 dt = opendir(dir_test);
926 error = got_error(GOT_ERR_NOT_GIT_REPO);
930 if ((asprintf(&dir_test, "%s/%s",
931 gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
932 return got_error_from_errno("asprintf");
934 gw_dir->path = strdup(dir_test);
937 gw_dir->description = gw_get_repo_description(gw_trans,
939 gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
940 gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
942 gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
951 static const struct got_error *
952 gw_load_got_paths(struct gw_trans *gw_trans)
954 const struct got_error *error = NULL;
956 struct dirent **sd_dent;
957 struct gw_dir *gw_dir;
959 unsigned int d_cnt, d_i;
961 d = opendir(gw_trans->gw_conf->got_repos_path);
963 error = got_error_from_errno2("opendir",
964 gw_trans->gw_conf->got_repos_path);
968 d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
971 error = got_error_from_errno2("scandir",
972 gw_trans->gw_conf->got_repos_path);
976 for (d_i = 0; d_i < d_cnt; d_i++) {
977 if (gw_trans->gw_conf->got_max_repos > 0 &&
978 (d_i - 2) == gw_trans->gw_conf->got_max_repos)
979 break; /* account for parent and self */
981 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
982 strcmp(sd_dent[d_i]->d_name, "..") == 0)
985 if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
986 return got_error_from_errno("gw_dir malloc");
988 error = gw_load_got_path(gw_trans, gw_dir);
989 if (error && error->code == GOT_ERR_NOT_GIT_REPO)
994 if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
995 !got_path_dir_is_empty(gw_dir->path)) {
996 TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
998 gw_trans->repos_total++;
1006 static const struct got_error *
1007 gw_parse_querystring(struct gw_trans *gw_trans)
1009 const struct got_error *error = NULL;
1011 struct gw_query_action *action = NULL;
1014 if (gw_trans->gw_req->fieldnmap[0]) {
1015 error = got_error_from_errno("bad parse");
1017 } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1018 /* define gw_trans->repo_path */
1019 if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
1020 return got_error_from_errno("asprintf");
1022 if ((asprintf(&gw_trans->repo_path, "%s/%s",
1023 gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
1024 return got_error_from_errno("asprintf");
1026 /* get action and set function */
1027 if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1028 for (i = 0; i < nitems(gw_query_funcs); i++) {
1029 action = &gw_query_funcs[i];
1030 if (action->func_name == NULL)
1033 if (strcmp(action->func_name,
1034 p->parsed.s) == 0) {
1035 gw_trans->action = i;
1036 if ((asprintf(&gw_trans->action_name,
1037 "%s", action->func_name)) == -1)
1039 got_error_from_errno(
1048 if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1049 if ((asprintf(&gw_trans->commit, "%s",
1050 p->parsed.s)) == -1)
1051 return got_error_from_errno("asprintf");
1053 if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1054 if ((asprintf(&gw_trans->repo_file, "%s",
1055 p->parsed.s)) == -1)
1056 return got_error_from_errno("asprintf");
1058 if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1059 if ((asprintf(&gw_trans->repo_folder, "%s",
1060 p->parsed.s)) == -1)
1061 return got_error_from_errno("asprintf");
1063 if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1064 if ((asprintf(&gw_trans->headref, "%s",
1065 p->parsed.s)) == -1)
1066 return got_error_from_errno("asprintf");
1068 if (action == NULL) {
1069 error = got_error_from_errno("invalid action");
1072 if ((gw_trans->gw_dir =
1073 gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1074 return got_error_from_errno("gw_dir malloc");
1076 error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1080 gw_trans->action = GW_INDEX;
1082 if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1083 gw_trans->page = p->parsed.i;
1085 /* if (gw_trans->action == GW_RAW) */
1086 /* gw_trans->mime = KMIME_TEXT_PLAIN; */
1091 static struct gw_dir *
1092 gw_init_gw_dir(char *dir)
1094 struct gw_dir *gw_dir;
1096 if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1099 if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1105 static const struct got_error *
1106 gw_display_open(struct gw_trans *gw_trans, enum khttp code, enum kmime mime)
1110 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1111 if (kerr != KCGI_OK)
1112 return gw_kcgi_error(kerr);
1113 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1115 if (kerr != KCGI_OK)
1116 return gw_kcgi_error(kerr);
1117 kerr = khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1119 if (kerr != KCGI_OK)
1120 return gw_kcgi_error(kerr);
1121 kerr = khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1122 if (kerr != KCGI_OK)
1123 return gw_kcgi_error(kerr);
1124 kerr = khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1125 if (kerr != KCGI_OK)
1126 return gw_kcgi_error(kerr);
1127 kerr = khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1128 if (kerr != KCGI_OK)
1129 return gw_kcgi_error(kerr);
1131 kerr = khttp_body(gw_trans->gw_req);
1132 return gw_kcgi_error(kerr);
1135 static const struct got_error *
1136 gw_display_index(struct gw_trans *gw_trans, const struct got_error *err)
1140 gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1141 kerr = khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1144 kerr = khttp_puts(gw_trans->gw_req, err->msg);
1146 kerr = khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1147 gw_query_funcs[gw_trans->action].template);
1148 if (kerr != KCGI_OK)
1149 return gw_kcgi_error(kerr);
1151 kerr = khtml_close(gw_trans->gw_html_req);
1152 return gw_kcgi_error(kerr);
1156 gw_template(size_t key, void *arg)
1158 const struct got_error *error = NULL;
1159 struct gw_trans *gw_trans = arg;
1160 char *gw_got_link, *gw_site_link;
1161 char *site_owner_name, *site_owner_name_h;
1165 khttp_puts(gw_trans->gw_req, head);
1168 gw_got_link = gw_get_got_link(gw_trans);
1169 if (gw_got_link != NULL)
1170 khttp_puts(gw_trans->gw_req, gw_got_link);
1174 case (TEMPL_SITEPATH):
1175 gw_site_link = gw_get_site_link(gw_trans);
1176 if (gw_site_link != NULL)
1177 khttp_puts(gw_trans->gw_req, gw_site_link);
1182 if (gw_trans->gw_conf->got_site_name != NULL)
1183 khtml_puts(gw_trans->gw_html_req,
1184 gw_trans->gw_conf->got_site_name);
1187 case (TEMPL_SEARCH):
1188 khttp_puts(gw_trans->gw_req, search);
1190 case(TEMPL_SITEOWNER):
1191 if (gw_trans->gw_conf->got_site_owner != NULL &&
1192 gw_trans->gw_conf->got_show_site_owner) {
1194 gw_html_escape(gw_trans->gw_conf->got_site_owner);
1195 if ((asprintf(&site_owner_name_h, site_owner,
1200 khttp_puts(gw_trans->gw_req, site_owner_name_h);
1201 free(site_owner_name);
1202 free(site_owner_name_h);
1205 case(TEMPL_CONTENT):
1206 error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1208 khttp_puts(gw_trans->gw_req, error->msg);
1217 gw_gen_commit_header(char *str1, char *str2)
1219 char *return_html = NULL, *ref_str = NULL;
1221 if (strcmp(str2, "") != 0) {
1222 if ((asprintf(&ref_str, "(%s)", str2)) == -1) {
1223 return_html = strdup("");
1227 ref_str = strdup("");
1230 if ((asprintf(&return_html, header_commit_html, str1, ref_str)) == -1)
1231 return_html = strdup("");
1238 gw_gen_diff_header(char *str1, char *str2)
1240 char *return_html = NULL;
1242 if ((asprintf(&return_html, header_diff_html, str1, str2)) == -1)
1243 return_html = strdup("");
1249 gw_gen_author_header(char *str)
1251 char *return_html = NULL;
1253 if ((asprintf(&return_html, header_author_html, str)) == -1)
1254 return_html = strdup("");
1260 gw_gen_committer_header(char *str)
1262 char *return_html = NULL;
1264 if ((asprintf(&return_html, header_committer_html, str)) == -1)
1265 return_html = strdup("");
1271 gw_gen_age_header(char *str)
1273 char *return_html = NULL;
1275 if ((asprintf(&return_html, header_age_html, str)) == -1)
1276 return_html = strdup("");
1282 gw_gen_commit_msg_header(char *str)
1284 char *return_html = NULL;
1286 if ((asprintf(&return_html, header_commit_msg_html, str)) == -1)
1287 return_html = strdup("");
1293 gw_gen_tree_header(char *str)
1295 char *return_html = NULL;
1297 if ((asprintf(&return_html, header_tree_html, str)) == -1)
1298 return_html = strdup("");
1304 gw_get_repo_description(struct gw_trans *gw_trans, char *dir)
1307 char *description = NULL, *d_file = NULL;
1310 if (gw_trans->gw_conf->got_show_repo_description == false)
1313 if ((asprintf(&d_file, "%s/description", dir)) == -1)
1316 if ((f = fopen(d_file, "r")) == NULL)
1319 fseek(f, 0, SEEK_END);
1321 fseek(f, 0, SEEK_SET);
1322 if ((description = calloc(len, sizeof(char *))) == NULL)
1325 fread(description, 1, len, f);
1330 if ((asprintf(&description, "%s", "")) == -1)
1337 gw_get_time_str(time_t committer_time, int ref_tm)
1341 char *years = "years ago", *months = "months ago";
1342 char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1343 char *minutes = "minutes ago", *seconds = "seconds ago";
1344 char *now = "right now";
1350 diff_time = time(NULL) - committer_time;
1351 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1352 if ((asprintf(&repo_age, "%lld %s",
1353 (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1355 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1356 if ((asprintf(&repo_age, "%lld %s",
1357 (diff_time / 60 / 60 / 24 / (365 / 12)),
1360 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1361 if ((asprintf(&repo_age, "%lld %s",
1362 (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1364 } else if (diff_time > 60 * 60 * 24 * 2) {
1365 if ((asprintf(&repo_age, "%lld %s",
1366 (diff_time / 60 / 60 / 24), days)) == -1)
1368 } else if (diff_time > 60 * 60 * 2) {
1369 if ((asprintf(&repo_age, "%lld %s",
1370 (diff_time / 60 / 60), hours)) == -1)
1372 } else if (diff_time > 60 * 2) {
1373 if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1376 } else if (diff_time > 2) {
1377 if ((asprintf(&repo_age, "%lld %s", diff_time,
1381 if ((asprintf(&repo_age, "%s", now)) == -1)
1386 if (gmtime_r(&committer_time, &tm) == NULL)
1389 s = asctime_r(&tm, datebuf);
1393 if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1401 gw_get_repo_age(struct gw_trans *gw_trans, char *dir, char *repo_ref,
1404 const struct got_error *error = NULL;
1405 struct got_object_id *id = NULL;
1406 struct got_repository *repo = NULL;
1407 struct got_commit_object *commit = NULL;
1408 struct got_reflist_head refs;
1409 struct got_reflist_entry *re;
1410 struct got_reference *head_ref;
1412 time_t committer_time = 0, cmp_time = 0;
1413 const char *refname;
1414 char *repo_age = NULL;
1416 SIMPLEQ_INIT(&refs);
1418 if (repo_ref == NULL)
1421 if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1424 if (gw_trans->gw_conf->got_show_repo_age == false) {
1425 if ((asprintf(&repo_age, "")) == -1)
1430 error = got_repo_open(&repo, dir, NULL);
1435 error = got_ref_list(&refs, repo, "refs/heads",
1436 got_ref_cmp_by_name, NULL);
1438 error = got_ref_list(&refs, repo, repo_ref,
1439 got_ref_cmp_by_name, NULL);
1443 SIMPLEQ_FOREACH(re, &refs, entry) {
1445 refname = strdup(repo_ref);
1447 refname = got_ref_get_name(re->ref);
1448 error = got_ref_open(&head_ref, repo, refname, 0);
1452 error = got_ref_resolve(&id, repo, head_ref);
1453 got_ref_close(head_ref);
1457 error = got_object_open_as_commit(&commit, repo, id);
1462 got_object_commit_get_committer_time(commit);
1464 if (cmp_time < committer_time)
1465 cmp_time = committer_time;
1468 if (cmp_time != 0) {
1469 committer_time = cmp_time;
1470 repo_age = gw_get_time_str(committer_time, ref_tm);
1472 if ((asprintf(&repo_age, "")) == -1)
1474 got_ref_list_free(&refs);
1478 if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1485 gw_get_diff(struct gw_trans *gw_trans, struct gw_header *header)
1487 const struct got_error *error;
1489 struct got_object_id *id1 = NULL, *id2 = NULL;
1490 struct buf *diffbuf = NULL;
1491 char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1492 *buf_color = NULL, *n_buf = NULL, *newline = NULL;
1500 error = buf_alloc(&diffbuf, 0);
1504 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
1508 if (strncmp(header->parent_id, "/dev/null", 9) != 0) {
1509 error = got_repo_match_object_id(&id1, &label1,
1510 header->parent_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1515 error = got_repo_match_object_id(&id2, &label2,
1516 header->commit_id, GOT_OBJ_TYPE_ANY, 1, header->repo);
1520 error = got_object_get_type(&obj_type, header->repo, id2);
1524 case GOT_OBJ_TYPE_BLOB:
1525 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL, 3, 0,
1528 case GOT_OBJ_TYPE_TREE:
1529 error = got_diff_objects_as_trees(id1, id2, "", "", 3, 0,
1532 case GOT_OBJ_TYPE_COMMIT:
1533 error = got_diff_objects_as_commits(id1, id2, 3, 0,
1537 error = got_error(GOT_ERR_OBJ_TYPE);
1540 if ((buf = calloc(128, sizeof(char *))) == NULL)
1543 fseek(f, 0, SEEK_SET);
1545 while ((fgets(buf, 2048, f)) != NULL) {
1547 while (*n_buf == '\n')
1549 newline = strchr(n_buf, '\n');
1553 buf_color = gw_colordiff_line(gw_html_escape(n_buf));
1554 if (buf_color == NULL)
1557 error = buf_puts(&newsize, diffbuf, buf_color);
1561 error = buf_puts(&newsize, diffbuf, div_end);
1566 if (buf_len(diffbuf) > 0) {
1567 error = buf_putc(diffbuf, '\0');
1568 diff_html = strdup(buf_get(diffbuf));
1587 gw_get_repo_owner(struct gw_trans *gw_trans, char *dir)
1590 char *owner = NULL, *d_file = NULL;
1591 char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1592 char *comp, *pos, *buf;
1595 if (gw_trans->gw_conf->got_show_repo_owner == false)
1598 if ((asprintf(&d_file, "%s/config", dir)) == -1)
1601 if ((f = fopen(d_file, "r")) == NULL)
1604 if ((buf = calloc(128, sizeof(char *))) == NULL)
1607 while ((fgets(buf, 128, f)) != NULL) {
1608 if ((pos = strstr(buf, gotweb)) != NULL)
1611 if ((pos = strstr(buf, gitweb)) != NULL)
1620 } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1625 if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1628 for (i = 0; i < 2; i++) {
1629 owner = strsep(&buf, "\"");
1639 if ((asprintf(&owner, "%s", "")) == -1)
1646 gw_get_clone_url(struct gw_trans *gw_trans, char *dir)
1649 char *url = NULL, *d_file = NULL;
1652 if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1655 if ((f = fopen(d_file, "r")) == NULL)
1658 fseek(f, 0, SEEK_END);
1660 fseek(f, 0, SEEK_SET);
1662 if ((url = calloc(len, sizeof(char *))) == NULL)
1665 fread(url, 1, len, f);
1672 gw_get_repo_tags(struct gw_trans *gw_trans, struct gw_header *header, int limit,
1675 const struct got_error *error = NULL;
1676 struct got_repository *repo = NULL;
1677 struct got_reflist_head refs;
1678 struct got_reflist_entry *re;
1679 char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1682 struct buf *diffbuf = NULL;
1685 SIMPLEQ_INIT(&refs);
1687 error = buf_alloc(&diffbuf, 0);
1691 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1695 error = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
1699 SIMPLEQ_FOREACH(re, &refs, entry) {
1700 const char *refname;
1701 char *refstr, *tag_commit0, *tag_commit, *id_str;
1704 struct got_object_id *id;
1705 struct got_tag_object *tag;
1707 refname = got_ref_get_name(re->ref);
1708 if (strncmp(refname, "refs/tags/", 10) != 0)
1711 refstr = got_ref_to_str(re->ref);
1712 if (refstr == NULL) {
1713 error = got_error_from_errno("got_ref_to_str");
1717 error = got_ref_resolve(&id, repo, re->ref);
1720 error = got_object_open_as_tag(&tag, repo, id);
1725 tagger = got_object_tag_get_tagger(tag);
1726 tagger_time = got_object_tag_get_tagger_time(tag);
1728 error = got_object_id_str(&id_str,
1729 got_object_tag_get_object_id(tag));
1733 tag_commit0 = strdup(got_object_tag_get_message(tag));
1735 if (tag_commit0 == NULL) {
1736 error = got_error_from_errno("strdup");
1740 tag_commit = tag_commit0;
1741 while (*tag_commit == '\n')
1746 newline = strchr(tag_commit, '\n');
1750 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1752 error = got_error_from_errno("asprintf");
1756 if ((asprintf(&tags_navs_disp, tags_navs,
1757 gw_trans->repo_name, id_str, gw_trans->repo_name,
1758 id_str, gw_trans->repo_name, id_str,
1759 gw_trans->repo_name, id_str)) == -1) {
1760 error = got_error_from_errno("asprintf");
1764 if ((asprintf(&tag_row, tags_row, age, refname,
1765 tag_commit, tags_navs_disp)) == -1) {
1766 error = got_error_from_errno("asprintf");
1770 free(tags_navs_disp);
1773 if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1775 error = got_error_from_errno("asprintf");
1778 if ((asprintf(&tag_row, tag_info, age,
1779 gw_html_escape(tagger),
1780 gw_html_escape(tag_commit))) == -1) {
1781 error = got_error_from_errno("asprintf");
1789 got_object_tag_close(tag);
1791 error = buf_puts(&newsize, diffbuf, tag_row);
1799 if (error || (limit && --limit == 0))
1803 if (buf_len(diffbuf) > 0) {
1804 error = buf_putc(diffbuf, '\0');
1805 tags = strdup(buf_get(diffbuf));
1809 got_ref_list_free(&refs);
1811 got_repo_close(repo);
1819 gw_free_headers(struct gw_header *header)
1823 if (header->commit != NULL)
1824 got_object_commit_close(header->commit);
1826 got_repo_close(header->repo);
1827 free(header->refs_str);
1828 free(header->commit_id);
1829 free(header->parent_id);
1830 free(header->tree_id);
1831 free(header->author);
1832 free(header->committer);
1833 free(header->commit_msg);
1836 static struct gw_header *
1839 struct gw_header *header;
1841 header = malloc(sizeof(*header));
1845 header->repo = NULL;
1846 header->commit = NULL;
1848 header->path = NULL;
1849 SIMPLEQ_INIT(&header->refs);
1854 static const struct got_error *
1855 gw_get_commits(struct gw_trans * gw_trans, struct gw_header *header,
1858 const struct got_error *error = NULL;
1859 struct got_commit_graph *graph = NULL;
1861 error = got_commit_graph_open(&graph, header->path, 0);
1865 error = got_commit_graph_iter_start(graph, header->id, header->repo,
1871 error = got_commit_graph_iter_next(&header->id, graph,
1872 header->repo, NULL, NULL);
1874 if (error->code == GOT_ERR_ITER_COMPLETED)
1878 if (header->id == NULL)
1881 error = got_object_open_as_commit(&header->commit, header->repo,
1886 error = gw_get_commit(gw_trans, header);
1888 struct gw_header *n_header = NULL;
1889 if ((n_header = gw_init_header()) == NULL)
1890 error = got_error_from_errno("malloc");
1892 n_header->refs_str = strdup(header->refs_str);
1893 n_header->commit_id = strdup(header->commit_id);
1894 n_header->parent_id = strdup(header->parent_id);
1895 n_header->tree_id = strdup(header->tree_id);
1896 n_header->author = strdup(header->author);
1897 n_header->committer = strdup(header->committer);
1898 n_header->commit_msg = strdup(header->commit_msg);
1899 n_header->committer_time = header->committer_time;
1900 TAILQ_INSERT_TAIL(&gw_trans->gw_headers, n_header,
1903 if (error || (limit && --limit == 0))
1908 got_commit_graph_close(graph);
1912 static const struct got_error *
1913 gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
1915 const struct got_error *error = NULL;
1916 struct got_reflist_entry *re;
1917 struct got_object_id *id2 = NULL;
1918 struct got_object_qid *parent_id;
1919 char *refs_str = NULL,
1920 *commit_msg = NULL, *commit_msg0;
1923 SIMPLEQ_FOREACH(re, &header->refs, entry) {
1926 struct got_tag_object *tag = NULL;
1929 name = got_ref_get_name(re->ref);
1930 if (strcmp(name, GOT_REF_HEAD) == 0)
1932 if (strncmp(name, "refs/", 5) == 0)
1934 if (strncmp(name, "got/", 4) == 0)
1936 if (strncmp(name, "heads/", 6) == 0)
1938 if (strncmp(name, "remotes/", 8) == 0)
1940 if (strncmp(name, "tags/", 5) == 0) {
1941 error = got_object_open_as_tag(&tag, header->repo,
1944 if (error->code != GOT_ERR_OBJ_TYPE)
1947 * Ref points at something other
1954 cmp = got_object_id_cmp(tag ?
1955 got_object_tag_get_object_id(tag) : re->id, header->id);
1957 got_object_tag_close(tag);
1961 if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1962 s ? ", " : "", name)) == -1) {
1963 error = got_error_from_errno("asprintf");
1967 header->refs_str = strdup(refs_str);
1971 if (refs_str == NULL)
1972 header->refs_str = strdup("");
1975 error = got_object_id_str(&header->commit_id, header->id);
1979 error = got_object_id_str(&header->tree_id,
1980 got_object_commit_get_tree_id(header->commit));
1984 if (gw_trans->action == GW_DIFF) {
1985 parent_id = SIMPLEQ_FIRST(
1986 got_object_commit_get_parent_ids(header->commit));
1987 if (parent_id != NULL) {
1988 id2 = got_object_id_dup(parent_id->id);
1990 error = got_object_id_str(&header->parent_id, id2);
1995 header->parent_id = strdup("/dev/null");
1997 header->parent_id = strdup("");
1999 header->committer_time =
2000 got_object_commit_get_committer_time(header->commit);
2002 if (gw_trans->action != GW_BRIEFS && gw_trans->action != GW_SUMMARY) {
2003 header->author = strdup(
2004 gw_html_escape(got_object_commit_get_author(header->commit))
2007 header->author = strdup(
2008 got_object_commit_get_author(header->commit)
2012 header->committer = strdup(
2013 gw_html_escape(got_object_commit_get_committer(header->commit))
2016 error = got_object_commit_get_logmsg(&commit_msg0, header->commit);
2020 commit_msg = commit_msg0;
2021 while (*commit_msg == '\n')
2024 header->commit_msg = strdup(commit_msg);
2029 static const struct got_error *
2030 gw_get_header(struct gw_trans *gw_trans, struct gw_header *header, int limit)
2032 const struct got_error *error = NULL;
2033 char *in_repo_path = NULL;
2035 error = got_repo_open(&header->repo, gw_trans->repo_path, NULL);
2039 if (gw_trans->commit == NULL) {
2040 struct got_reference *head_ref;
2041 error = got_ref_open(&head_ref, header->repo,
2042 gw_trans->headref, 0);
2046 error = got_ref_resolve(&header->id, header->repo, head_ref);
2047 got_ref_close(head_ref);
2051 error = got_object_open_as_commit(&header->commit,
2052 header->repo, header->id);
2054 struct got_reference *ref;
2055 error = got_ref_open(&ref, header->repo, gw_trans->commit, 0);
2056 if (error == NULL) {
2058 error = got_ref_resolve(&header->id, header->repo, ref);
2062 error = got_object_get_type(&obj_type, header->repo,
2066 if (obj_type == GOT_OBJ_TYPE_TAG) {
2067 struct got_tag_object *tag;
2068 error = got_object_open_as_tag(&tag,
2069 header->repo, header->id);
2072 if (got_object_tag_get_object_type(tag) !=
2073 GOT_OBJ_TYPE_COMMIT) {
2074 got_object_tag_close(tag);
2075 error = got_error(GOT_ERR_OBJ_TYPE);
2079 header->id = got_object_id_dup(
2080 got_object_tag_get_object_id(tag));
2081 if (header->id == NULL)
2082 error = got_error_from_errno(
2083 "got_object_id_dup");
2084 got_object_tag_close(tag);
2087 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2088 error = got_error(GOT_ERR_OBJ_TYPE);
2091 error = got_object_open_as_commit(&header->commit,
2092 header->repo, header->id);
2096 if (header->commit == NULL) {
2097 error = got_repo_match_object_id_prefix(&header->id,
2098 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2103 error = got_repo_match_object_id_prefix(&header->id,
2104 gw_trans->commit, GOT_OBJ_TYPE_COMMIT,
2108 error = got_repo_map_path(&in_repo_path, header->repo,
2109 gw_trans->repo_path, 1);
2114 header->path = strdup(in_repo_path);
2118 error = got_ref_list(&header->refs, header->repo, NULL,
2119 got_ref_cmp_by_name, NULL);
2123 error = gw_get_commits(gw_trans, header, limit);
2131 char datebuf[11]; /* YYYY-MM-DD + NUL */
2134 struct gw_blame_cb_args {
2135 struct blame_line *lines;
2139 off_t *line_offsets;
2141 struct got_repository *repo;
2142 struct gw_trans *gw_trans;
2143 struct buf *blamebuf;
2146 static const struct got_error *
2147 gw_blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2149 const struct got_error *err = NULL;
2150 struct gw_blame_cb_args *a = arg;
2151 struct blame_line *bline;
2153 size_t linesize = 0, newsize;
2154 struct got_commit_object *commit = NULL;
2157 time_t committer_time;
2159 if (nlines != a->nlines ||
2160 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2161 return got_error(GOT_ERR_RANGE);
2164 return NULL; /* no change in this commit */
2166 /* Annotate this line. */
2167 bline = &a->lines[lineno - 1];
2168 if (bline->annotated)
2170 err = got_object_id_str(&bline->id_str, id);
2174 err = got_object_open_as_commit(&commit, a->repo, id);
2178 bline->committer = strdup(got_object_commit_get_committer(commit));
2179 if (bline->committer == NULL) {
2180 err = got_error_from_errno("strdup");
2184 committer_time = got_object_commit_get_committer_time(commit);
2185 if (localtime_r(&committer_time, &tm) == NULL)
2186 return got_error_from_errno("localtime_r");
2187 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2188 &tm) >= sizeof(bline->datebuf)) {
2189 err = got_error(GOT_ERR_NO_SPACE);
2192 bline->annotated = 1;
2194 /* Print lines annotated so far. */
2195 bline = &a->lines[a->lineno_cur - 1];
2196 if (!bline->annotated)
2199 offset = a->line_offsets[a->lineno_cur - 1];
2200 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2201 err = got_error_from_errno("fseeko");
2205 while (bline->annotated) {
2206 char *smallerthan, *at, *nl, *committer, *blame_row = NULL,
2207 *line_escape = NULL;
2210 if (getline(&line, &linesize, a->f) == -1) {
2212 err = got_error_from_errno("getline");
2216 committer = bline->committer;
2217 smallerthan = strchr(committer, '<');
2218 if (smallerthan && smallerthan[1] != '\0')
2219 committer = smallerthan + 1;
2220 at = strchr(committer, '@');
2223 len = strlen(committer);
2225 committer[8] = '\0';
2227 nl = strchr(line, '\n');
2231 if (strcmp(line, "") != 0)
2232 line_escape = strdup(gw_html_escape(line));
2234 line_escape = strdup("");
2236 asprintf(&blame_row, blame_line, a->nlines_prec,
2237 a->lineno_cur, bline->id_str, bline->datebuf, committer,
2240 err = buf_puts(&newsize, a->blamebuf, blame_row);
2244 bline = &a->lines[a->lineno_cur - 1];
2250 got_object_commit_close(commit);
2256 gw_get_file_blame(struct gw_trans *gw_trans)
2258 const struct got_error *error = NULL;
2259 struct got_repository *repo = NULL;
2260 struct got_object_id *obj_id = NULL;
2261 struct got_object_id *commit_id = NULL;
2262 struct got_blob_object *blob = NULL;
2263 char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2265 struct gw_blame_cb_args bca;
2269 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2273 if (gw_trans->repo_folder != NULL) {
2274 if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2275 error = got_error_from_errno("asprintf");
2279 folder = strdup("");
2281 if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2282 error = got_error_from_errno("asprintf");
2287 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2291 error = got_repo_match_object_id(&commit_id, NULL, gw_trans->commit,
2292 GOT_OBJ_TYPE_COMMIT, 1, repo);
2296 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2300 if (obj_id == NULL) {
2301 error = got_error(GOT_ERR_NO_OBJ);
2305 error = got_object_get_type(&obj_type, repo, obj_id);
2309 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2310 error = got_error(GOT_ERR_OBJ_TYPE);
2314 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2318 error = buf_alloc(&bca.blamebuf, 0);
2322 bca.f = got_opentemp();
2323 if (bca.f == NULL) {
2324 error = got_error_from_errno("got_opentemp");
2327 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2328 &bca.line_offsets, bca.f, blob);
2329 if (error || bca.nlines == 0)
2332 /* Don't include \n at EOF in the blame line count. */
2333 if (bca.line_offsets[bca.nlines - 1] == filesize)
2336 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2337 if (bca.lines == NULL) {
2338 error = got_error_from_errno("calloc");
2342 bca.nlines_prec = 0;
2349 bca.gw_trans = gw_trans;
2351 error = got_blame(in_repo_path, commit_id, repo, gw_blame_cb, &bca,
2353 if (buf_len(bca.blamebuf) > 0) {
2354 error = buf_putc(bca.blamebuf, '\0');
2355 blame_html = strdup(buf_get(bca.blamebuf));
2365 error = got_object_blob_close(blob);
2367 error = got_repo_close(repo);
2371 for (i = 0; i < bca.nlines; i++) {
2372 struct blame_line *bline = &bca.lines[i];
2373 free(bline->id_str);
2374 free(bline->committer);
2378 free(bca.line_offsets);
2379 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2380 error = got_error_from_errno("fclose");
2388 gw_get_repo_tree(struct gw_trans *gw_trans)
2390 const struct got_error *error = NULL;
2391 struct got_repository *repo = NULL;
2392 struct got_object_id *tree_id = NULL, *commit_id = NULL;
2393 struct got_tree_object *tree = NULL;
2394 struct buf *diffbuf = NULL;
2396 char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2397 *tree_row = NULL, *id_str;
2400 error = buf_alloc(&diffbuf, 0);
2404 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2408 error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2412 if (gw_trans->repo_folder != NULL)
2413 path = strdup(gw_trans->repo_folder);
2414 else if (in_repo_path) {
2416 path = in_repo_path;
2419 if (gw_trans->commit == NULL) {
2420 struct got_reference *head_ref;
2421 error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2425 error = got_ref_resolve(&commit_id, repo, head_ref);
2426 got_ref_close(head_ref);
2429 error = got_repo_match_object_id(&commit_id, NULL,
2430 gw_trans->commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
2434 error = got_object_id_str(&gw_trans->commit, commit_id);
2438 error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2442 error = got_object_open_as_tree(&tree, repo, tree_id);
2446 nentries = got_object_tree_get_nentries(tree);
2448 for (i = 0; i < nentries; i++) {
2449 struct got_tree_entry *te;
2450 const char *modestr = "";
2451 char *id = NULL, *url_html = NULL;
2453 te = got_object_tree_get_entry(tree, i);
2455 error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2459 if ((asprintf(&id, "%s", id_str)) == -1) {
2460 error = got_error_from_errno("asprintf");
2465 mode_t mode = got_tree_entry_get_mode(te);
2467 if (got_object_tree_entry_is_submodule(te))
2469 else if (S_ISLNK(mode))
2471 else if (S_ISDIR(mode))
2473 else if (mode & S_IXUSR)
2476 char *build_folder = NULL;
2477 if (S_ISDIR(got_tree_entry_get_mode(te))) {
2478 if (gw_trans->repo_folder != NULL) {
2479 if ((asprintf(&build_folder, "%s/%s",
2480 gw_trans->repo_folder,
2481 got_tree_entry_get_name(te))) == -1) {
2483 got_error_from_errno("asprintf");
2487 if (asprintf(&build_folder, "%s",
2488 got_tree_entry_get_name(te)) == -1)
2492 if ((asprintf(&url_html, folder_html,
2493 gw_trans->repo_name, gw_trans->action_name,
2494 gw_trans->commit, build_folder,
2495 got_tree_entry_get_name(te), modestr)) == -1) {
2496 error = got_error_from_errno("asprintf");
2500 if (gw_trans->repo_folder != NULL) {
2501 if ((asprintf(&build_folder, "%s",
2502 gw_trans->repo_folder)) == -1) {
2504 got_error_from_errno("asprintf");
2508 build_folder = strdup("");
2510 if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2511 "blame", gw_trans->commit,
2512 got_tree_entry_get_name(te), build_folder,
2513 got_tree_entry_get_name(te), modestr)) == -1) {
2514 error = got_error_from_errno("asprintf");
2523 if ((asprintf(&tree_row, tree_line, url_html)) == -1) {
2524 error = got_error_from_errno("asprintf");
2527 error = buf_puts(&newsize, diffbuf, tree_row);
2537 if (buf_len(diffbuf) > 0) {
2538 error = buf_putc(diffbuf, '\0');
2539 tree_html = strdup(buf_get(diffbuf));
2543 got_object_tree_close(tree);
2545 got_repo_close(repo);
2557 gw_get_repo_heads(struct gw_trans *gw_trans)
2559 const struct got_error *error = NULL;
2560 struct got_repository *repo = NULL;
2561 struct got_reflist_head refs;
2562 struct got_reflist_entry *re;
2563 char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2564 struct buf *diffbuf = NULL;
2567 SIMPLEQ_INIT(&refs);
2569 error = buf_alloc(&diffbuf, 0);
2573 error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2577 error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2582 SIMPLEQ_FOREACH(re, &refs, entry) {
2585 refname = strdup(got_ref_get_name(re->ref));
2586 if (refname == NULL) {
2587 error = got_error_from_errno("got_ref_to_str");
2591 if (strncmp(refname, "refs/heads/", 11) != 0) {
2596 age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2599 if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2600 refname, gw_trans->repo_name, refname,
2601 gw_trans->repo_name, refname, gw_trans->repo_name,
2603 error = got_error_from_errno("asprintf");
2607 if (strncmp(refname, "refs/heads/", 11) == 0)
2610 if ((asprintf(&head_row, heads_row, age, refname,
2611 head_navs_disp)) == -1) {
2612 error = got_error_from_errno("asprintf");
2616 error = buf_puts(&newsize, diffbuf, head_row);
2618 free(head_navs_disp);
2622 if (buf_len(diffbuf) > 0) {
2623 error = buf_putc(diffbuf, '\0');
2624 heads = strdup(buf_get(diffbuf));
2628 got_ref_list_free(&refs);
2630 got_repo_close(repo);
2638 gw_get_got_link(struct gw_trans *gw_trans)
2642 if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2643 gw_trans->gw_conf->got_logo)) == -1)
2650 gw_get_site_link(struct gw_trans *gw_trans)
2652 char *link, *repo = "", *action = "";
2654 if (gw_trans->repo_name != NULL)
2655 if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2656 "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2659 if (gw_trans->action_name != NULL)
2660 if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2663 if ((asprintf(&link, site_link, GOTWEB,
2664 gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2671 gw_colordiff_line(char *buf)
2673 const struct got_error *error = NULL;
2674 char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2675 struct buf *diffbuf = NULL;
2678 error = buf_alloc(&diffbuf, 0);
2684 if (strncmp(buf, "-", 1) == 0)
2685 color = "diff_minus";
2686 if (strncmp(buf, "+", 1) == 0)
2687 color = "diff_plus";
2688 if (strncmp(buf, "@@", 2) == 0)
2689 color = "diff_chunk_header";
2690 if (strncmp(buf, "@@", 2) == 0)
2691 color = "diff_chunk_header";
2692 if (strncmp(buf, "commit +", 8) == 0)
2693 color = "diff_meta";
2694 if (strncmp(buf, "commit -", 8) == 0)
2695 color = "diff_meta";
2696 if (strncmp(buf, "blob +", 6) == 0)
2697 color = "diff_meta";
2698 if (strncmp(buf, "blob -", 6) == 0)
2699 color = "diff_meta";
2700 if (strncmp(buf, "file +", 6) == 0)
2701 color = "diff_meta";
2702 if (strncmp(buf, "file -", 6) == 0)
2703 color = "diff_meta";
2704 if (strncmp(buf, "from:", 5) == 0)
2705 color = "diff_author";
2706 if (strncmp(buf, "via:", 4) == 0)
2707 color = "diff_author";
2708 if (strncmp(buf, "date:", 5) == 0)
2709 color = "diff_date";
2711 if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2714 error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2718 error = buf_puts(&newsize, diffbuf, buf);
2722 if (buf_len(diffbuf) > 0) {
2723 error = buf_putc(diffbuf, '\0');
2724 colorized_line = strdup(buf_get(diffbuf));
2728 free(div_diff_line_div);
2729 return colorized_line;
2733 gw_html_escape(const char *html)
2735 char *escaped_str = NULL, *buf;
2737 size_t sz, i, buff_sz = 2048;
2739 if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2745 if ((sz = strlen(html)) == 0)
2748 /* only work with buff_sz */
2752 for (i = 0; i < sz; i++) {
2756 strcat(buf, ">");
2759 strcat(buf, "&");
2762 strcat(buf, "<");
2765 strcat(buf, """);
2768 strcat(buf, "'");
2771 strcat(buf, "<br />");
2777 asprintf(&escaped_str, "%s", buf);
2783 main(int argc, char *argv[])
2785 const struct got_error *error = NULL;
2786 struct gw_trans *gw_trans;
2787 struct gw_dir *dir = NULL, *tdir;
2788 const char *page = "index";
2792 if ((gw_trans = malloc(sizeof(struct gw_trans))) == NULL)
2795 if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2798 if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2801 if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2804 kerr = khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX, &page, 1, 0);
2805 if (kerr != KCGI_OK) {
2806 error = gw_kcgi_error(kerr);
2810 if ((gw_trans->gw_conf =
2811 malloc(sizeof(struct gotweb_conf))) == NULL) {
2813 error = got_error_from_errno("malloc");
2817 TAILQ_INIT(&gw_trans->gw_dirs);
2818 TAILQ_INIT(&gw_trans->gw_headers);
2821 gw_trans->repos_total = 0;
2822 gw_trans->repo_path = NULL;
2823 gw_trans->commit = NULL;
2824 gw_trans->headref = strdup(GOT_REF_HEAD);
2825 gw_trans->mime = KMIME_TEXT_HTML;
2826 gw_trans->gw_tmpl->key = gw_templs;
2827 gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2828 gw_trans->gw_tmpl->arg = gw_trans;
2829 gw_trans->gw_tmpl->cb = gw_template;
2830 error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2834 error = gw_parse_querystring(gw_trans);
2838 error = gw_display_index(gw_trans, error);
2841 gw_trans->mime = KMIME_TEXT_PLAIN;
2842 gw_trans->action = GW_ERR;
2843 gw_display_index(gw_trans, error);
2846 free(gw_trans->gw_conf->got_repos_path);
2847 free(gw_trans->gw_conf->got_site_name);
2848 free(gw_trans->gw_conf->got_site_owner);
2849 free(gw_trans->gw_conf->got_site_link);
2850 free(gw_trans->gw_conf->got_logo);
2851 free(gw_trans->gw_conf->got_logo_url);
2852 free(gw_trans->gw_conf);
2853 free(gw_trans->commit);
2854 free(gw_trans->repo_path);
2855 free(gw_trans->repo_name);
2856 free(gw_trans->repo_file);
2857 free(gw_trans->action_name);
2858 free(gw_trans->headref);
2860 TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2862 free(dir->description);
2871 khttp_free(gw_trans->gw_req);
2872 return EXIT_SUCCESS;