Who knows libcurl?

BlitzMax Forums/BlitzMax Beginners Area/Who knows libcurl?

LuckyBargee(Posted 2011) [#1]
Hi All,

I'm trying to upload some files to the server, but I've got messages and I have no idea where to go:

' -----------------
220 ProFTPD 1.3.2 Server (ProFTPD) [77.222.146.41]
331 Password required for 135369
230 User 135369 logged in
257 "/" is the current directory
***** URL using bad/illegal format or missing URL *****
Process complete
' -----------------

My code is here:

Framework BaH.libcurl
Import BRL.StandardIO
Import BRL.FileSystem

Local res:Int =0		
Local curl:TCurlEasy 	
Local upload:UploadHelper= UploadHelper.Create("ftptest.txt")

curl = TCurlEasy.Create()
curl.setOptInt(CURLOPT_VERBOSE, 1)
curl.setDebugCallback(debugFunction)

curl.setOptString(CURLOPT_URL, "ftp://135369:nip@...")
curl.setOptString(CURLOPT_USERPWD, "135369:nip")
curl.setOptInt(CURLOPT_UPLOAD, True)
curl.setOptLong(CURLOPT_INFILESIZE_LARGE, upload.size)
curl.setReadCallback(upload.Read, upload)
res = curl.perform()
If res Then
	Print "***** " + CurlError(res) + " *****"
End If
curl.cleanup()
End 


Where was my mistake?
What does it mean ""/" is the current directory"?
How to input the path correctly?
Help, pls.


xlsior(Posted 2011) [#2]
What does it mean ""/" is the current directory"?


It's Linux/Unix folder designation.
"/" is the main/root folder on a system.
Could be the 'real' root, or could be a virtual folder designation associated with the account that is logged in.

(the system may -tell- the user it is in / , while in reality it mapped it over to /root/web/site/youraccount)


Where was my mistake?


I've never used libcur, or looked at its documentation, but looking at the above it seems odd to me to have the username/password as part of both the connection URL, and specified again on the next line with CURLOPT_USERPWD -- have you tried simply removing it from the URL , leaving just the hostname itself in there?

How to input the path correctly?


I'd assume you can specify the folder at the end of the URL, e.g. ftp://mysite.com/use/this/folder


LuckyBargee(Posted 2011) [#3]
Thank you xlsior,

I'd assume you can specify the folder at the end of the URL, e.g. ftp://mysite.com/use/this/folder


Well, I changed
curl.setOptString(CURLOPT_URL, "ftp://135369:nip@...")

into
curl.setOptString(CURLOPT_URL, "ftp://dl10.upload.com.ua")

and the rest I left the same.

Here is the result:
' -----------
220 ProFTPD 1.3.2 Server (ProFTPD) [77.222.146.41]
331 Password required for 135369
230 User 135369 logged in
257 "/" is the current directory
***** URL using bad/illegal format or missing URL *****
Process complete
' -----------

What is wrong?


ima747(Posted 2011) [#4]
You have no path in the URL? not familiar with libcurl but it likely wants atleast some path component, try with a trailing slash i.e.

ftp://dl10.upload.com.ua/


LuckyBargee(Posted 2011) [#5]
Thank you ima747,


You have no path in the URL? not familiar with libcurl but it likely wants atleast some path component, try with a trailing slash i.e.
ftp://dl10.upload.com.ua/



I did it. The response was the same.
:(


jkrankie(Posted 2011) [#6]
Isn't you username and password the wrong way round? It's been a while since i used curl, but typically APIs want username:password rather than the other way round.

Cheers
Charlie


LuckyBargee(Posted 2011) [#7]
The pair username and password is correct.
If I rearrange them and put instead of "135369:nip" something like "nip:135369", I get:


220 ProFTPD 1.3.2 Server (ProFTPD) [77.222.146.41]
331 Password required for nip
530 Login incorrect.
***** Login denied *****
Process complete



Doesn't anybody use the libcurl by Bruce?

Ok.

Maybe is there a less complicated way (than libcurl) to upload a file on the server? Could you share it, and show an example with my server (http://upload.com.ua, username and password are 135369 and nip), pls?


jsp(Posted 2011) [#8]
Htbaa does use it for his Maximus Client, may try to contact him.
http://www.blitzmax.com/Community/posts.php?topic=95621#1102660


Htbaa(Posted 2011) [#9]
I've never used libcurl for FTP stuff. But I believe in the documentation Brucey has added some examples.

PS: You might want to edit out your username/password from the examples (if they aren't fake).


LuckyBargee(Posted 2011) [#10]
Thank you Htbaa,

The first my post is right from Brucey's example! What do you use for FTP?


Htbaa(Posted 2011) [#11]
I don't use FTP for Maximus. With Maximus files are downloaded with the HTTP protocol.

If you have access to the FTP server through SSH or another way to access the logs I'd suggest you check out the ProFTPD logfiles.


jsp(Posted 2011) [#12]
I didn't use libcurl yet, but played a bit with your code and it looks like you just forgot to specify the new file path and name you want to transfer.



the output is then
220 ProFTPD 1.3.2 Server (ProFTPD) [77.222.146.41]

331 Password required for 135369

230 User 135369 logged in

257 "/" is the current directory

229 Entering Extended Passive Mode (|||26742|)

200 Type set to I

150 Opening BINARY mode data connection for test




I don't have your uploadHelper type and have commented it out but it looks promising.

Be aware that your current ftp site only allows to transfers files, but not to create subdirectories.

Hope this helps


LuckyBargee(Posted 2011) [#13]
Thank you a lot, jsp!


it looks like you just forgot to specify the new file path and name you want to transfer.


You right! Question was how to put the path in the code.

I tryed you code, but got error. Could you run the example (here is the full code with uploadHelper from Brucey):


SuperStrict

Framework BaH.libcurl
Import BRL.StandardIO
Import BRL.FileSystem


Local res:Int =0		
Local curl:TCurlEasy 	
Local upload:UploadHelper= UploadHelper.Create("ftptest.txt")

curl = TCurlEasy.Create()
curl.setOptInt(CURLOPT_VERBOSE, 1)
curl.setDebugCallback(debugFunction)

' ftp connect and initial directory
curl.setOptString(CURLOPT_URL, "<a href=\"mailto:ftp://135369:nip@dl10.upload.com.ua\">ftp://135369:nip@...;")
'curl.setOptString(CURLOPT_URL, "ftp://dl10.upload.com.ua/")
curl.setOptString(CURLOPT_USERPWD, "nip:135369")
curl.setOptInt(CURLOPT_UPLOAD, True)
curl.setOptLong(CURLOPT_INFILESIZE_LARGE, upload.size)
curl.setReadCallback(upload.Read, upload)
res = curl.perform()
If res Then
	Print "***** " + CurlError(res) + " *****"
End If
curl.cleanup()
End 


' for our debugging
Function debugFunction:Int(data:Object, msgType:Int, message:String)

	' we only care about server info
	If msgType = CURLINFO_HEADER_IN Then
		Print message
	End If

End Function

' This just processes a filestream for us.
' DoRead, does the hard work of feeding the callback buffer with data from the stream.
Type UploadHelper

	Field name:String
	Field size:Int
	Field currentPos:Int
	
	Field stream:TStream

	Function Create:UploadHelper(file:String)
		Local this:UploadHelper = New UploadHelper
		
		this.name = file
		this.size = FileSize(file)
		
		Print " FILE NAME IS: " + This.name
		If this.size=-1
			Print "* CAN'T FIND THE FILE!"
		Else
			
			Print " FILE SIZE IS " + this.size
		EndIf 
		
		Return this
	End Function
	
	Method DoRead:Int(buffer:Byte Ptr, size:Int)
		If Not stream Then
			stream = ReadStream(name)
			currentPos = 0
		End If
		
		Local count:Int = Stream.Read( buffer, size )
		currentPos :+ count
		
		Return count
	End Method

	Function Read:Int(buffer:Byte Ptr, size:Int, data:Object)
		Return UploadHelper(data).DoRead(buffer, size)
	End Function

End Type



jsp(Posted 2011) [#14]
Either the account has been locked or the passwd was changed on your ftp site.
Maybe there were too many wrong attempts to login.
Please check


LuckyBargee(Posted 2011) [#15]
Well, on that chance I decided to change the server.
I went to hotfile.com (file share server) and created a test account (Username:SergeyIvanov Password:ivanovsergey). Then I changed the code with new username and password:

curl.setOptString(CURLOPT_URL, "ftp.hotfile.com")
curl.setOptString(CURLOPT_USERPWD, "SergeyIvanov:ivanovsergey")


And now my result is the same:

220 HotFile v0.2
331 Password required for user SergeyIvanov
230 User SergeyIvanov logged in
257 "/" is the current directory
***** URL using bad/illegal format or missing URL *****
Process complete



What should I do?


jsp(Posted 2011) [#16]
Works here!



gives:

Building Up
Compiling:Up.bmx
flat assembler version 1.68 (575513 kilobytes memory)
3 passes, 3908 bytes.
Linking:Up.exe
Executing:Up.exe
FILE NAME IS: ftptest.txt
FILE SIZE IS 4
220 HotFile v0.2

331 Password required for user SergeyIvanov

230 User SergeyIvanov logged in

257 "/" is the current directory

500 Unknown command EPSV

227 Entering Passive Mode (74,120,9,186,27,219).

200 type set to I

150 Opening data connection for ftptest.txt

226 Transfer complete

221 See ya


Process complete




LuckyBargee(Posted 2011) [#17]
Wow, it really works! I start to like you, curl!
Thank you, jsp!

While I searched an answer for my question I dug into a lot of papers about ftp and curl. I came across with a very interesting file (rfc2396), describing ftp formats. I thought it would be very useful for all newcomers, like me
:) A fragment is here:


but now ... I have stuck with a new sophisticated problem. I need to get my own (external) IP address and I looked through the blitzmax forum for it.
I saw an advice from Filax:
ReadLine(ReadFile("http::www.whatismyip.com/automation/n09230945.asp"))

http://blitzmax.com/Community/posts.php?topic=51389#817489
but it didn't work (Null object).

Then I met Vertex lib. But I wanted to use Curl for it. Is it possible? May be someone did it or knows how to do it. Please, share.

Thus, question is how to get my own (external, WAN) IP address by curl?

Thank you.


jsp(Posted 2011) [#18]
Use this:

Print ReadLine(ReadFile("http::automation.whatismyip.com/n09230945.asp"))

that should do the trick.


LuckyBargee(Posted 2011) [#19]
Sorry, but the trick doesn't work!

"Unhandled Exception: Attempt to access field or method of Null object!"

:(


jsp(Posted 2011) [#20]
Not sure what you are doing, but that works perfectly here.
Split the command in several lines and check where the error comes from.
Also just drop the "automation.whatismyip.com/n09230945.asp" into your browser and check if you get your IP back.


LuckyBargee(Posted 2011) [#21]
Thank you for help!

My code is here:


It really doesn't work! BUT IT WORKS PERFECTLY IF I TAKE AWAY "BaH.libcurl" STRING ! like this:
'Framework BaH.libcurl

However, I need the string for further! What should I do to run the code with libcur?


xlsior(Posted 2011) [#22]
Try using:


Framework BaH.libcurl
Import BRL.StandardIO
Import BRL.Stream
Import BRL.FileSystem



when you remove the 'framework' line and just keep the imports, then it means that blitzmax will use the default framework which includes *all* modules.

since you're now using TStreams, you also need to add the import for BRL.Stream to make it work.

I'd suggest you download a copy of Framework Assistant, which will tell you all the modules that your program depends on.


LuckyBargee(Posted 2011) [#23]
Thank you, xlsior!

I put Import BRL.FileSystem:


Unfortunately, it still doesn't work!

ReadFile("http::automation.whatismyip.com/n09230945.asp") returns Null!

Framework Assistant says:

Framework BRL.FileSystem
Import BRL.StandardIO
Import BRL.Stream



???


xlsior(Posted 2011) [#24]
Oops, looks like you need one more:

import brl.httpstream


Note that if you have a (software) firewall, you may also need to allow your program permission to access the internet before it can retrieve the information for you.


LuckyBargee(Posted 2011) [#25]
Wow!

It's unbelievable, but.. it works! How did you get to know about it?

Thank you a lot, xlsior! Thanks to All !