summaryrefslogtreecommitdiff
path: root/cgitcgi/ui-file.c
blob: bf335b35a78cb12ce120a5d2bbbb48713476e58b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include <stddef.h>   /* offsetof(3) */
#include <dirent.h>
#include <sys/stat.h> /* chmod(2)    */
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>   /* strdup(3)   */
#include <libgen.h>   /* basename(3) */
#include <ctype.h>    /* tolower(3)  */
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <locale.h>
#include <unistd.h>

#include <md4c.h>
#include <md4c-html.h>

#include <git2.h>

#include <nls.h>

#include <defs.h>

#include <fatal.h>
#include <http.h>
#include <html.h>

#include <dlist.h>
#include <strbuf.h>
#include <repolist.h>
#include <wrapper.h>
#include <system.h>
#include <date.h>

#include <ctx.h>
#include <git-shared.h>
#include <ui-shared.h>


static void cgit_print_file_links( struct strbuf *sb, const char *relative_path, const char *revision, int top )
{
  const char *query_string = NULL;
  const char *place = NULL;

  if( !sb || !relative_path ) return;

  if( top )
    place = "top";
  else
    place = "bottom";

  strbuf_addf( sb, "<div class=\"%s file-links-menu\">\n", place );
  strbuf_addf( sb, "  <div class=\"right links-menu\">\n" );

  query_string = ctx_remove_query_param( ctx.env.query_string, "rev" );
  query_string = ctx_remove_query_param( query_string, "op" );

  if( query_string && *query_string )
  {
    strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-scroll\"></span><a href=\"/%s/%s/?op=log&rev=%s&%s\">log</a></div>\n", ctx.repo.name, relative_path, revision, query_string );
    strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-diff\"></span><a href=\"/%s/%s/?op=diff&rev=%s&%s\">diff</a></div>\n", ctx.repo.name, relative_path, revision, query_string );
    strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-blame\"></span><a href=\"/%s/%s/?op=blame&rev=%s&%s\">blame</a></div>\n", ctx.repo.name, relative_path, revision, query_string );
  }
  else
  {
    strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-scroll\"></span><a href=\"/%s/%s/?op=log&rev=%s\">log</a></div>\n", ctx.repo.name, relative_path, revision );
    strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-diff\"></span><a href=\"/%s/%s/?op=diff&rev=%s\">diff</a></div>\n", ctx.repo.name, relative_path, revision );
    strbuf_addf( sb, "    <div class=\"item\"><span class=\"icon las la-blame\"></span><a href=\"/%s/%s/?op=blame&rev=%s\">blame</a></div>\n", ctx.repo.name, relative_path, revision );
  }

  strbuf_addf( sb, "  </div>\n" );
  strbuf_addf( sb, "</div>\n" );
}


static void process_markdown_output( const MD_CHAR *text, MD_SIZE size, void *sb )
{
  strbuf_add( (struct strbuf *)sb, (const void *)text, (size_t)size );
}

static int cgit_write_markdown_content( struct strbuf *sb, const struct strbuf *input )
{
  unsigned parser_flags   = MD_DIALECT_GITHUB; /* | MD_DIALECT_COMMONMARK */
  unsigned renderer_flags = MD_FLAG_WIKILINKS; /* | MD_HTML_FLAG_DEBUG    */

  /*********************************
    md_html() returns 0 on success:
   */
  return md_html( input->buf, input->len, process_markdown_output,
                  (void *)sb, parser_flags, renderer_flags );
}


static void cgit_print_file( struct strbuf *sb, const char *relative_path, const char *revision )
{
  struct cgit_info info;

  if( !sb || !relative_path ) return;

  if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
  {
    cgit_rpath_info( &info, relative_path, revision );
    if( info.kind != GIT_OBJECT_BLOB )
    {
      strbuf_addf( sb, "              <h1>Requested resource cannot be shown</h1>\n" );
      strbuf_addf( sb, "              <p class='leading'>File with specified revision '%s' cannot be shown.</p>\n", revision );
      return;
    }
  }
  else
  {
    memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
  }

  cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 1 );

  if( info.lang )
  {
    if( !strcmp( info.lang, "Markdown" ) )
      strbuf_addstr( sb, "<div class='markdown-content'>" );
    else
      strbuf_addf( sb, "<pre><code class='language-%s'>", info.lang );
  }
  else
    strbuf_addstr( sb, "<pre><code class='highlight'>" );

  if( info.kind == GIT_OBJECT_BLOB )
  {
    git_repository *repo = NULL;
    git_blob       *blob = NULL;
    git_oid         oid;
    /* git_off_t       rawsize = 0; */
    const char     *rawcontent = NULL;

    if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 )
    {
      if( info.lang && !strcmp( info.lang, "Markdown" ) )
        strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
      else
        strbuf_addstr( sb, "\n</code></pre>\n" );

      cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
      return;
    }

    if( !(repo = cgit_open_repository()) )
    {
      if( info.lang && !strcmp( info.lang, "Markdown" ) )
        strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
      else
        strbuf_addstr( sb, "\n</code></pre>\n" );

      cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
      return;
    }

    if( git_blob_lookup( &blob, repo, &oid ) < 0 )
    {
      close_repository( repo );

      if( info.lang && !strcmp( info.lang, "Markdown" ) )
        strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
      else
        strbuf_addstr( sb, "\n</code></pre>\n" );

      cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
      return;
    }

    /* rawsize = git_blob_rawsize( blob ); */
    rawcontent = (const char *)git_blob_rawcontent( blob );

    if( !git_blob_is_binary( blob ) && rawcontent && *rawcontent )
    {
      struct strbuf buf = STRBUF_INIT;

      if( info.lang && !strcmp( info.lang, "Markdown" ) )
      {
        strbuf_addstr( &buf, (const char *)rawcontent );
        (void)cgit_write_markdown_content( sb, (const struct strbuf *)&buf );
        strbuf_release( &buf );
      }
      else
      {
        strbuf_addstr_xml_quoted( &buf, (const char *)rawcontent );
        strbuf_addbuf( sb, (const struct strbuf *)&buf );
        strbuf_release( &buf );
      }
    }
    else
    {
      git_blob_free( blob );
      close_repository( repo );

      if( info.lang && !strcmp( info.lang, "Markdown" ) )
        strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
      else
        strbuf_addstr( sb, "\n</code></pre>\n" );

      cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );
      return;
    }

    git_blob_free( blob );
    close_repository( repo );
  }

  if( info.lang && !strcmp( info.lang, "Markdown" ) )
    strbuf_addstr( sb, "\n</div> <!-- End of Markdown Content -->\n" );
  else
    strbuf_addstr( sb, "\n</code></pre>\n" );

  cgit_print_file_links( sb, relative_path, (const char *)&info.revision[0], 0 );

  return;
}

static void cgit_print_image_file( struct strbuf *sb, const char *relative_path, const char *revision )
{
  struct cgit_info info;

  if( !sb || !relative_path ) return;

  if( revision && strcmp( revision, (const char *)&ctx.repo.relative_info.revision[0] ) )
  {
    cgit_rpath_info( &info, relative_path, revision );
    if( info.kind != GIT_OBJECT_BLOB )
    {
      strbuf_addf( sb, "              <h1>Invalid Revision</h1>\n" );
      strbuf_addf( sb, "              <p class='leading'>File with specified revision '%s' cannot be shown.</p>\n", revision );
      return;
    }
  }
  else
  {
    memcpy( (void *)&info, (void *)&ctx.repo.relative_info, sizeof( struct cgit_info ) );
  }

  if( info.kind == GIT_OBJECT_BLOB )
  {
    git_repository *repo = NULL;
    git_blob       *blob = NULL;
    git_oid         oid;
    git_off_t       rawsize = 0;
    const char     *rawcontent = NULL;

    if( git_oid_fromstr( &oid, (const char *)&info.oid[0] ) < 0 ) return;
    if( !(repo = cgit_open_repository()) ) return;

    if( git_blob_lookup( &blob, repo, &oid ) < 0 )
    {
      close_repository( repo );
      return;
    }

    rawsize = git_blob_rawsize( blob );
    rawcontent = (const char *)git_blob_rawcontent( blob );

    if( rawcontent && *rawcontent )
    {
      struct strbuf buf = STRBUF_INIT;

      strbuf_add( &buf, (const void *)rawcontent, (size_t)rawsize );

      git_blob_free( blob );
      close_repository( repo );

      /****************************************************
        This call should terminate the program on success:
       */
      cgit_print_raw_file( &buf, info.mime );

      strbuf_release( &buf );
    }
    else
    {
      git_blob_free( blob );
      close_repository( repo );
      return;
    }

    git_blob_free( blob );
    close_repository( repo );
  }

  return;
}

void cgit_print_file_page( void )
{
  FILE  *fp;
  struct strbuf buf = STRBUF_INIT;

  fp = xfopen( ctx.page.header, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
  strbuf_addf( &buf, "          <div class=\"container\">\n" );
  strbuf_addf( &buf, "            <div class=\"cgit-main-content\">\n" );

  if( ctx.repo.relative_info.kind == GIT_OBJECT_BLOB )
  {
    if( !strncmp( ctx.repo.relative_info.mime, "text/", 5 ) )
    {
      if( ctx.repo.name )
      {
        cgit_print_file( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );
      }
      else
      {
        strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
        strbuf_addf( &buf, "              <p class='leading'>Repository '%s' not found.</p>\n", ctx.repo.name );
      }
    }
    else if( !strncmp( ctx.repo.relative_info.mime, "image/", 6 ) )
    {
      /****************************************************
        This call should terminate the program on success:
       */
      cgit_print_image_file( &buf, ctx.repo.relative_path, (!strcmp( ctx.query.rev, "0" )) ? (const char *)&ctx.repo.relative_info.revision[0] : ctx.query.rev );

      strbuf_addf( &buf, "              <h1>Requested file cannot be shown</h1>\n" );
      strbuf_addf( &buf, "              <p class='leading'>Files with mime type such as '%s' cannot be present.</p>\n", ctx.repo.relative_info.mime );
    }
    else
    {
      strbuf_addf( &buf, "              <h1>Requested file cannot be shown</h1>\n" );
      strbuf_addf( &buf, "              <p class='leading'>Files with mime type such as '%s' cannot be present.</p>\n", ctx.repo.relative_info.mime );
    }
  }
  else
  {
    strbuf_addf( &buf, "              <h1>Requested resource cannot be shown</h1>\n" );
    strbuf_addf( &buf, "              <p class='leading'>This page assume plain text files to be present.</p>\n" );
  }

  strbuf_addf( &buf, "            </div> <!-- End of cgit-main-content -->\n" );
  strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
  strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );

  fp = xfopen( ctx.page.footer, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  ctx.page.size = buf.len;
  cgit_print_http_headers();
  strbuf_write( &buf, STDOUT_FILENO );
  strbuf_release( &buf );
}