# HG changeset patch # User Aleksej Bobylev # Date 1489979549 -7200 # Node ID eff71eaaee4ff9145f2ae25355f2ea2fa1023a00 # Parent 34b05a9e92c725721e1f6ba130681afeca90ba07 web/cooker.cgi: work with HTTP headers "Last-Modified", "If-Modified-Since", "HTTP 304 Not Modified", "HTTP 404 Not Found". Other small improvements. SliTaz Next Cooker is working right now with this version of cooker.cgi. diff -r 34b05a9e92c7 -r eff71eaaee4f web/cooker.cgi --- a/web/cooker.cgi Fri Mar 17 02:42:26 2017 +0200 +++ b/web/cooker.cgi Mon Mar 20 05:12:29 2017 +0200 @@ -23,16 +23,124 @@ cooktime="$CACHE/cooktime" wokrev="$CACHE/wokrev" -# Path to sundown (markdown to html convertor) -if [ -n "$(which sundown 2>/dev/null)" ]; then - SUNDOWN=$(which sundown) +# Path to markdown to html convertor +if [ -n "$(which cmark 2>/dev/null)" ]; then + md2html="$(which cmark) --smart" +elif [ -x "./cmark" ]; then + md2html="./cmark --smart" +elif [ -n "$(which sundown 2>/dev/null)" ]; then + md2html=$(which sundown) elif [ -x "./sundown" ]; then - SUNDOWN="./sundown" + md2html="./sundown" fi # We're not logged and want time zone to display correct server date. export TZ=$(cat /etc/TZ) + +# HTML page header. Pages can be customized with a separated header.html file + +page_header() { + echo -e 'Content-Type: text/html; charset=UTF-8\n' + if [ -f "header.html" ]; then + cat header.html + else + cat < + + + + + SliTaz Cooker + + + + + + + + + + +EOT + fi +} + + +# HTML page footer. Pages can be customized with a separated footer.html file + +page_footer() { + if [ -f "footer.html" ]; then + cat footer.html + else + cat < + + + + + +EOT + fi +} + + +not_found() { + local file="${1#$PKGS/}"; file="${file#$LOGS/}"; file="${file#$WOK/}" + echo "HTTP/1.1 404 Not Found" + page_header + echo "

Not Found

" + case $2 in + pkg) + echo "

The requested package $(basename "$(dirname "$file")") was not found on this server.

" ;; + *) + echo "

The requested file $file was not found on this server.

" ;; + esac + page_footer +} + + +manage_modified() { + local file="$1" option="$2" nul day mon year time hh mm ss date_s + if [ ! -f "$file" ]; then + if [ "$option" == 'silently-absent' ]; then + echo "HTTP/1.1 404 Not Found" + return + else + not_found "$file" "$2" + exit + fi + fi + [ "$option" == 'no-last-modified' ] && return + if [ -n "$HTTP_IF_MODIFIED_SINCE" ]; then + echo "$HTTP_IF_MODIFIED_SINCE" | \ + while read nul day mon year time nul; do + case $mon in + Jan) mon='01';; Feb) mon='02';; Mar) mon='03';; Apr) mon='04';; + May) mon='05';; Jun) mon='06';; Jul) mon='07';; Aug) mon='08';; + Sep) mon='09';; Oct) mon='10';; Nov) mon='11';; Dec) mon='12';; + esac + hh=$(echo $time | cut -d: -f1) + mm=$(echo $time | cut -d: -f2) + ss=$(echo $time | cut -d: -f3) + date_s=$(date -ud "$year$mon$day$hh$mm.$ss" +%s) + if [ "$date_s" -ge "$(date -ur "$file" +%s)" ]; then + echo -e 'HTTP/1.1 304 Not Modified\n' + exit + fi + done + fi + echo "Last-Modified: $(date -Rur "$file" | sed 's|UTC|GMT|')" +} + + case "$QUERY_STRING" in recook=*) case "$HTTP_USER_AGENT" in @@ -40,36 +148,39 @@ grep -qs "^${QUERY_STRING#recook=}$" $CACHE/recook-packages || echo ${QUERY_STRING#recook=} >> $CACHE/recook-packages esac - cat <|\>|g' -} - # Put some colors in log and DB files. syntax_highlighter() { @@ -248,19 +375,6 @@ s|\[0m||g;" ;; - receipt) - sed -e s'|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|'g \ - -e s"#^\#\([^']*\)#\0#"g \ - -e s"#\"\([^']*\)\"#\0#"g - ;; - - diff) - sed -e 's|&|\&|g' -e 's|<|\<|g' -e 's|>|\>|g' \ - -e s"#^-\([^']*\).#\0#"g \ - -e s"#^+\([^']*\).#\0#"g \ - -e s"#@@\([^']*\)@@#@@\1@@#"g - ;; - activity) sed s"#^\([^']* : \)#\0#"g ;; @@ -268,6 +382,13 @@ } +show_code() { + echo "
"
+	sed 's|&|\&|g; s|<|\<|g; s|>|\>|g'
+	echo '
' +} + + # Latest build pkgs. list_packages() { @@ -426,45 +547,17 @@ -# xHTML header. Pages can be customized with a separated html.header file. - -if [ -f "header.html" ]; then - cat header.html -else - cat < - - - - - SliTaz Cooker - - - - - - - - - - -EOT -fi - # # Load requested page # +page_header + case "${QUERY_STRING}" in pkg=*) pkg=${QUERY_STRING#pkg=} log=$LOGS/$pkg.log - echo "
" - echo "

Package: $pkg

" # Define cook variables for syntax highlighter if [ -s "$WOK/$pkg/receipt" ]; then @@ -478,11 +571,15 @@ # Package info. if [ -f "$wok/$pkg/receipt" ]; then + echo "
" + echo "

Package: $pkg

" pkg_info else if [ $(ls $wok/*$pkg*/receipt 2>/dev/null | wc -l) -eq 0 ]; then - echo "No package named: $pkg" + echo "

Not Found

" + echo "

The requested package $pkg was not found on this server.

" else + echo "
" ls $wok/$pkg/receipt >/dev/null 2>&1 || pkg="*$pkg*" echo '' for i in $(cd $wok ; ls $pkg/receipt); do @@ -504,6 +601,8 @@ # Check for a log file and display summary if it exists. summary "$log" + + # Display button only for SliTaz web browser if [ -f "$log" ]; then case "$HTTP_USER_AGENT" in *SliTaz*) @@ -571,17 +670,6 @@ echo '' ;; - *.diff) - diff=$CACHE/$file - echo "

Diff for: ${file%.diff}

" - [ "$file" == "installed.diff" ] && echo \ - "

This is the latest diff between installed packages \ - and installed build dependencies to cook.

" - echo '
'
-				cat $diff | syntax_highlighter diff
-				echo '
' - ;; - *.log) log=$LOGS/$file name=$(basename $log) @@ -619,9 +707,9 @@ done case $file in - *.desktop|*.theme) class="ini" ;; + *.desktop|*.theme) class="ini" ;; *.patch|*.diff|*.u) class="diff" ;; - *.sh) class="bash" ;; + *.sh) class="bash" ;; *.conf*) class="bash" [ -n "$(cut -c1 < $wok/$file | fgrep '[')" ] && class="ini" @@ -658,23 +746,19 @@ ;; esac - # Display colored listing (also for *.svg) + # Display colored listing for all text-based documents (also for *.svg) case $file in *.png|*.jpg|*.jpeg|*.ico) ;; *) if [ -z "$raw" ]; then - echo -n "
"
-						cat $wok/$file | sed 's|&|\&|g; s|<|\<|g; s|>|\>|g'
-						echo '
' + cat $wok/$file | show_code $class fi ;; esac # Display hex dump for binary files if [ -n "$raw" ]; then - echo -n "
"
-				hexdump -C $wok/$file | sed 's|&|\&|g; s|<|\<|g; s|>|\>|g'
-				echo '
' + hexdump -C $wok/$file | show_code $class fi else echo "
File '$file' absent!
" @@ -684,67 +768,52 @@ receipt=*) echo "
" pkg=${QUERY_STRING#receipt=} - if [ -f "$wok/$pkg/receipt" ]; then - echo "

Receipt for: $pkg

" - pkg_info - echo "receipt" - . $wok/$pkg/receipt + echo "

Receipt for: $pkg

" + pkg_info + echo "receipt" + . $wok/$pkg/receipt - ( cd $wok/$pkg; find stuff -type f 2> /dev/null ) | sort | \ - while read file; do - echo "$file" - done | sort - echo -n '
'
-			cat $wok/$pkg/receipt | htmlize
-			echo '
' - else - echo "
No receipt for: $pkg
" - fi + ( cd $wok/$pkg; find stuff -type f 2> /dev/null ) | sort | \ + while read file; do + echo "$file" + done | sort + cat $wok/$pkg/receipt | show_code bash ;; files=*) echo "
" pkg=${QUERY_STRING#files=} dir=$(ls -d $WOK/$pkg/taz/$pkg-* 2>/dev/null) - if [ -d "$dir/fs" ]; then - size=$(du -hs $dir/fs | awk '{ print $1 }') - echo "

Files installed by the package \"$pkg\" ($size)

" - pkg_info - #echo "‹ back" + size=$(du -hs $dir/fs | awk '{ print $1 }') + echo "

Files installed by the package \"$pkg\" ($size)

" + pkg_info - echo '
'
+		echo '
'
 
-			find $dir/fs -not -type d -print0 | \
-			xargs -0 ls -ld --color=always | \
-			syntax_highlighter files | \
-			sed "s|\([^/]*\)/.*\(${dir#*wok}/fs\)\([^<]*\)\(<.*\)$|\1\3\4|"
+		find $dir/fs -not -type d -print0 | sort -z | \
+		xargs -0 ls -ld --color=always | \
+		syntax_highlighter files | \
+		sed "s|\([^/]*\)/.*\(${dir#*wok}/fs\)\([^<]*\)\(<.*\)$|\1\3\4|"
 
-			echo '
' - else - echo "

No files list for \"$pkg\"

" - echo "‹ back" - fi + echo '
' ;; description=*) echo "
" pkg=${QUERY_STRING#description=} dir=$(ls -d $WOK/$pkg/taz/$pkg-* 2>/dev/null) + echo "

Description of $pkg

" + pkg_info if [ -s "$dir/description.txt" ]; then - echo "

Description of $pkg

" - pkg_info - if [ -x "$SUNDOWN" ]; then + if [ -n "$md2html" ]; then echo '
' - $SUNDOWN $dir/description.txt + $md2html $dir/description.txt echo '
' else - echo '
'
-				cat $dir/description.txt | \
-					sed 's/&/\&/g;s//\>/g'
-				echo '
' + cat $dir/description.txt | show_code markdown fi else - echo "
No description for: $pkg
" + echo "
No description of $pkg
" fi ;; @@ -752,93 +821,99 @@ echo '
' type=${QUERY_STRING%%=*} pkg=$(GET $type) - if [ -d "$wok/$pkg" ]; then - dir=$WOK/$pkg/install/usr/share/$type - [ -d $dir ] || dir=$WOK/$pkg/install/usr/$type - [ -d $dir ] || dir=$(echo $WOK/$pkg/taz/*/fs/usr/share/$type) - [ -d $dir ] || dir=$(echo $WOK/$pkg/taz/*/fs/usr/$type) - page=$(GET file) - if [ -z "$page" ]; then - page=$(find $dir -type f | sed q) - page=${page#$dir/} - fi + dir=$WOK/$pkg/install/usr/share/$type + [ -d $dir ] || dir=$WOK/$pkg/install/usr/$type + [ -d $dir ] || dir=$(echo $WOK/$pkg/taz/*/fs/usr/share/$type) + [ -d $dir ] || dir=$(echo $WOK/$pkg/taz/*/fs/usr/$type) + page=$(GET file) + if [ -z "$page" ]; then + page=$(find $dir -type f | sed q) + page=${page#$dir/} + fi - echo "

$(basename $page)

" + echo "

$(basename $page)

" - pkg_info - echo '
' - find $dir -type f | sort | while read i ; do - [ -s $i ] || continue - case "$i" in - *.jp*g|*.png|*.gif|*.svg|*.css) continue - esac - i=${i#$dir/} - class=''; [ "$page" == "$i" ] && class=" plum" - echo "$(basename $i .gz)" - done | sort -t \> -k 2 - echo '
' + pkg_info + echo '
' + find $dir -type f | sort | while read i ; do + [ -s $i ] || continue + case "$i" in + *.jp*g|*.png|*.gif|*.svg|*.css) continue + esac + i=${i#$dir/} + class=''; [ "$page" == "$i" ] && class=" plum" + case "$type" in + man) + man=$(basename $i .gz) + echo "${man%.*} (${man##*.})" + ;; + info) + info=$(basename $i) + echo "${info/.info/}" + ;; + *) + echo "$(basename $i .gz)" + ;; + esac + done + echo '
' - if [ -f "$dir/$page" ]; then - tmp="$(mktemp)" - docat "$dir/$page" > $tmp - [ -s "$tmp" ] && - case "$type" in - info) - echo '
' - echo '
'
-						info2html < "$tmp"
-						echo '
' - ;; - doc) - case "$page" in - *.sgml) class='xml';; - *) class='asciidoc';; - esac - case "$page" in - *.htm*) - echo '
' - cat - echo '
' - ;; - *) - echo "
"
-								sed 's/&/\&/g;s//\>/g'
-								echo '
' - ;; - esac < "$tmp" - ;; - man) - export TEXTDOMAIN='man2html' - echo "
" + if [ -f "$dir/$page" ]; then + tmp="$(mktemp)" + docat "$dir/$page" > $tmp + [ -s "$tmp" ] && + case "$type" in + info) + echo '
' + echo '
'
+					info2html < "$tmp"
+					echo '
' + ;; + doc) + case "$page" in + *.sgml) class='xml';; + *) class='asciidoc';; + esac + case "$page" in + *.htm*) + echo '
' + cat + echo '
' + ;; + *) + show_code $class + ;; + esac < "$tmp" + ;; + man) + export TEXTDOMAIN='man2html' + echo "
" - html=$(./man2html "$tmp" | sed -e '1,/
/d' \ - -e 's|]*>\([^<]*\)|\1|g') + html=$(./man2html "$tmp" | sed -e '1,/
/d' \ + -e 's|]*>\([^<]*\)|\1|g') - if [ -n "$(echo "$html" | fgrep 'The requested file /tmp/tmp.')" ]; then - # Process the pre-formatted man-cat page - echo '
'
-							sed '
-								s|M-bM-^@M-^S|—|g;
-								s|M-bM-^@M-^\\||g;
-								s|M-bM-^@M-^]||g
-								s|M-bM-^@M-^X||g;
-								s|M-bM-^@M-^Y||g;
-								s|M-BM-||g;
-								' "$tmp"
-							echo '
' - else - echo "$html" - fi - echo "
" - ;; - esac - rm -f $tmp - else - echo "
File '$page' not exists!
" - fi + if [ -n "$(echo "$html" | fgrep 'The requested file /tmp/tmp.')" ]; then + # Process the pre-formatted man-cat page + echo '
'
+						sed '
+							s|M-bM-^@M-^S|—|g;
+							s|M-bM-^@M-^\\||g;
+							s|M-bM-^@M-^]||g
+							s|M-bM-^@M-^X||g;
+							s|M-bM-^@M-^Y||g;
+							s|M-BM-||g;
+							' "$tmp"
+						echo '
' + else + echo "$html" + fi + echo "
" + ;; + esac + rm -f $tmp else - echo "
Package '$pkg' not exists!
" + echo "
File '$page' not exists!
" fi ;; @@ -958,19 +1033,5 @@ esac -# Close xHTML page - -cat < - - - - - -EOT - +page_footer exit 0