haskell cgi web programming help - haskell

I have a form:
<div id="form">
<form action="upload.cgi" method="get" enctype="multipart/form-data">
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload"/>
</form>
</div>
and the upload.cgi is:
saveFile n =
do cont <- liftM fromJust $ getInputFPS "file"
let f = uploadDir ++ "/" ++ basename n
liftIO $ writeFile f cont
return $ paragraph << ("Saved")
uploadDir = "./uploadDirectory"
basename = reverse . takeWhile (`notElem` "/\\") . reverse
page t b = header << thetitle << t +++ body << b
myFromJust (Just a) = a
myFromJust Nothing = "gs"
cgiMain = do
mn <- getInputFilename "file"
h <- saveFile (myFromJust mn)
output . renderHtml $ page "Saved" h
main = runCGI $ handleErrors cgiMain
The problem is that every time I try to upload a file instead of saving that file, a file called gs is made(from myFromJust function, if I use fromJust I get Nothing and the program fails) whose contents is the uploaded file name.
So, I try to upload a file called something.zip and i get a file called gs whose contents is something.zip (written as text).
I think the problem is that the getInputFilename "file" or getInputFPS don't return anything but I dont't know why.
Is something wrong with the form or the program?

The problem is that the original code1 uses POST; but in the HTML Form of the question, HTML GET is used instead. The fix is to change GET to POST in the HTML.
From the original example where it uses HTTP POST:
fileForm =
form ! [method "post"
, enctype "multipart/form-data"]
<< [afile "file", submit "" "Upload"]
The new Form (partly below) where it improperly uses GET.
<form action="upload.cgi"
method="get" enctype="multipart/form-data">
Notes
1 See Section 9: Web/Literature/Practical web programming in Haskel

Related

How to read new line in a textarea form using bootstrap?

I´m making a form using twitter-bootstrap
One of them is a form control which supports multiple lines of text
<textarea class="form-control" id="resumen" rows="1"></textarea>
then I read the contents
texto = $('#resumen').val()+'\r\n'
for example if user enters into id resumen form:
hello world
hello world again
texto = hello world hello world again
How can I read new lines in the form ?
The full string is a line and do not read the 'new lines' added (using enter) by the user.
I would like to read the full string with diferent lines, as the user enter it
How can I do that?
New lines are there, if you read and print that input string in console you will see new lines are coming.
If you want to print that input string in html then replace those new lines by <br>,
If you want to play with each line break them into array
<textarea id="textarea"></textarea>
read
<script type="text/javascript">
function readTextArea () {
var data = $("#textarea").val();
var dataNlToBr = data.replace(/\r?\n/g, '<br>');
var dataArray = dataNlToBr .split('<br>');
console.log(data);
console.log(dataNlToBr);
console.log(dataArray);
}
</script>

How to get a part of a substring from a particular string in python

I have a string
str = <iframe width="420" height="315" src="https://www.youtube-nocookie.com/embed/jc8zayasOB8" frameborder="0" allowfullscreen></iframe>
and I want to take the substring in src - ie 'https://www.youtube-nocookie.com/embed/jc8zayasOB8'. So how can we achieve this.
I tried this
my_string="hello python world , i'm a beginner "
print my_string.split("world",1)[1] .
But it gives all string after world . I want only the substring inside src.
My solution:
string = '<iframe width="420" height="315" src="https://www.youtube-nocookie.com/embed/jc8zayasOB8" frameborder="0" allowfullscreen></iframe>'
start_src = string.find('src="')
first_quote = string.find('"', start_src)
end_quote = string.find('"', first_quote+1)
print string[first_quote+1:end_quote]

Adding string on one variable in classic asp

I'm trying logging verification, everything seems to be fine, but when I add the special characters string to another variable it causes an error to occur.
I'm trying to detect if a user is inputting special characters such as: !, . etc. something like that.
Here's my code,
<form action="" method="get"/>
username:<input type="text" name="user"/><br>
password:<input type="password" name="pass"/><br>
<input type="submit" value="Login">
</form>
<%
dim user,pass, spchar, getspchar
user=request.querystring("user")
pass=request.querystring("pass")
spchar = "!"
getspchar = spchar
if user = "" then
response.write("Please provide your first name")
elseif pass = "" then
response.write("Please provide your password")
elseif user = spchar or pass = spchar then
response.write(getspchar &" Special character not allowed")
elseif user <> "admin" or pass <> "admin" then
response.write("Invalid Username or Password")
else
response.write("welcome")
end if
%>
What you need to do is check to see if your special characters are in the list. Perhaps something like the following...
<%
Dim user, pass, specchars
'Put all your special characters in the following list...
user = Request.QueryString("user")
pass = Request.QueryString("pass")
specchars = "!£$%^"
If IsValid(user, specchars) And IsValid(pass, specchars) Then
Response.Write("Username and password are fine! Welcome!")
Else
Response.Write("Bad username or password.")
End if
'The reason I've given two arguments here is so that you can have different
'restricted characters for both the username and password...
Function IsValid(phrase, special)
Dim rv, c
For c = 1 to Len(specchars)
rv = (Instr(phrase, Mid(special, c, 1)) = 0)
Next
IsValid = rv
End Function
%>
Just as an aside, here, you're visually displaying your username and password in the query string which is tagged on to the end of your URL (something like www.example.com/default.asp?user=admin&pass=G0d); this isn't a good idea. Try at least using POST in your form instead of GET. If you do this, then you're going to have to look at changing to using Request.Form("controlname") ... and that's just scratching the surface.
Please remember that this is a very basic piece of code and I would not recommend using any structure like this for your security on the internet. You'll need to look into Secure Sockets Layer (SSL) and similar encryption.

Dictionary Object Losing its Contents Classic ASP

<%# language = "VBScript"%>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="">
Username:<br> <input type = "text" name = "user"/><br>
Password:<br> <input type = "password" name = "pass"/><br>
<input type = "submit" name = "register_button" value = "Register"/>
<input type = "submit" name = "submit_button" value = "Submit"/>
</form>
<%
set credentials = Server.CreateObject("Scripting.Dictionary")
set Session("credentials") = credentials
if Request.form("register_button") <> "" then
dim user
dim pass
pass = Request.form("pass")
user = Request.form("user")
Session("credentials").Add user, pass
end if
if Request.form("submit_button") <> "" then
dim userd
dim passd
passd = Request.form("pass")
userd = Request.form("user")
if Session("credentials").Exists(userd) = true then
if Session("credentials").Item(userd) = passd then
Response.Write("ACCESS GRANTED")
else
Response.Write("ACCESS DENIED")
end if
else
Response.Write("USERNAME DOES NOT EXIST")
end if
end if
%>
</body>
</html>
So this is a basic login page I created using Classic ASP. When I type in a user and pass, then click Register, it stores the username and password in the Dictionary no problem. But, when I click the submit button (after filling in the registered credentials), somehow the contents of the dictionary get deleted. So, it fails the "if Session("credentials").Exists(userd) = true then" statement. I even tried making "credentials" a normal variable, without using Session. It still won't work, the dictionary's contents just get deleted. How can I overcome this problem? Thanks in advance!
this first
credentials.Add user, pass
set Session("credentials") = credentials
should be after

Loading/Modifying/Saving Changes to Text File

Allow me to start by sharing what I have so far:
main :: IO ()
main = do contents <- readFile "filmList.txt"
let database = (read contents :: [Film])
putStr "Please enter your username: "
userName <- getLine
menu database
where menu newDb = do putStrLn "\nPlease select an option:"
putStrLn "1: Display all films currently in the database"
putStrLn "2: Add a new film to the database (and display all films)"
putStrLn "3: "
putStrLn "4: Save Database"
putStrLn "5: Exit"
putStr "\nSelected option: "
option <- getLine
case option of "1" -> putStrLn(displayFilms newDb)
"2" -> do putStr "Name of film: "
title <- getLine
putStr "Name of director: "
director <- getLine
putStr "Year of release: "
year <- getLine
putStrLn(displayFilms (addNewFilm title director (read year) newDb))
menu newDb
`
I have managed to load a txt file with the database of Film types but I cannot figure out how to go about actually making changes to the data. When I try to run option 2, I get a list of all the films with the newly added one as well but if I then run option 1 to list all films, it doesn't include the newly added film. Should I be saving back to the txt file each time a new Film instance is added? Any help is greatly appreciated, thank you!
Keep in mind that everything in Haskell is immutable. When you write addNewFilm (addNewFilm title director (read year) newDb, that doesn't change the database represented by newDb to include the new information; it merely returns a new database that has all the old information plus the new row. It's your responsibility to choose where and when to use the old database (named, ironically, newDb) or the new one returned by the function.
For example, you might write this case instead:
case option of "1" -> putStrLn (displayFilms newDb) >> menu newDb
"2" -> do ...
let evenMoreReallyNewDb = addNewFilm title director (read year) newDb
putStrLn (displayFilms evenMoreReallyNewDb)
menu evenMoreReallyNewDb
You might notice that both cases end with a very similar pattern (a putStrLn . displayFilms and then a menu). Unifying them would be a good idea; see if you can see how to do that yourself.
If you want to write the modified database back to the file, you will of course need to do that explicitly using writeFile or a similar function.

Resources