[Set Content-length header in outputFile. Added download example. Add Makefile in examples directory. Updated all examples to compile with the current library. bjorn@bringert.net**20060602044332] { hunk ./Network/NewCGI.hs 64 -import System.IO (Handle, hPutStrLn, +import System.IO (Handle, hPutStrLn, openFile, hFileSize, IOMode(ReadMode), hunk ./Network/NewCGI.hs 235 --- | Output the contents of a file lazily. The output is assumed to be text\/html, --- encoded using ISO-8859-1. To change this, set the +-- | Output the contents of a file lazily. The output is assumed +-- to be text\/html, encoded using ISO-8859-1. To change this, set the hunk ./Network/NewCGI.hs 238 +-- The file must be a regular file, since this function looks at its +-- size to set the Content-length header. To output the contents of +-- non-regular files, use 'outputFPS'. hunk ./Network/NewCGI.hs 244 -outputFile f = liftIO (BS.readFile f) >>= outputFPS +outputFile f = do (c,sz) <- liftIO $ contentsAndSize f + setHeader "Content-length" (show sz) + outputFPS c + where contentsAndSize x = do h <- openFile x ReadMode + sz <- hFileSize h + c <- BS.hGetContents h + return (c,sz) addfile ./examples/Makefile hunk ./examples/Makefile 1 - +PROGS = upload.cgi download.cgi printinput.cgi redirect.cgi multipart-extract + +.PHONY: all clean + +all: $(PROGS) + +%.cgi: %.hs + ghc --make -o $@ $^ + +%: %.hs + ghc --make -o $@ $^ + +clean: + -rm -f *.hi *.o + -rm -f $(PROGS) addfile ./examples/download.hs hunk ./examples/download.hs 1 +#!/usr/bin/env runghc + +{-# OPTIONS_GHC -package cgi #-} + +-- | Takes server file path from the file parameter and sends +-- that to the client. +-- WARNING: this script is a SECURITY RISK and only for +-- demo purposes. Do not put it on a public web server. +module Main where + +import Control.Monad (liftM) +import Data.Maybe (fromJust) + +import Network.NewCGI + +download = + do m <- getInput "file" + case m of + Just n -> do + setHeader "Content-type" "application/octet-stream" + outputFile n + Nothing -> printForm + +printForm = + output $ "
" + +main = runCGI download hunk ./examples/multipart-extract.hs 1 +-- | This program takes a boundary string and a file name as +-- the command line arguments and parses the +-- contents of the file as multipart\/form-data. The bodies of +-- all the message parts are printed to standard output. +module Main where + hunk ./examples/multipart-extract.hs 9 +import Data.ByteString.Lazy as BS + hunk ./examples/multipart-extract.hs 15 -readMultipart b h = do - inp <- hGetContents h - parseM (p_multipart_body b) "" inp +readMultipart b h = do inp <- BS.hGetContents h + case parseMultipartBody b inp of + Just x -> return x + Nothing -> fail "Couldn't parse input." hunk ./examples/multipart-extract.hs 21 -savePart (BodyPart hs c) = putStr c +savePart (BodyPart hs c) = BS.putStr c }