HTTP Post Help

Just starting out? Need help? Post your questions and find answers here.
blackborg
User
User
Posts: 38
Joined: Thu Nov 02, 2006 8:20 pm

HTTP Post Help

Post by blackborg »

Can anyone help me figure out why this post request is not uploading a file.. is there something incomplete? arghhh..

Code: Select all


Procedure.s GetLibID(xmlFile.s)
  Protected libID.s = ""
  xmlDoc = LoadXML(#PB_Any,xmlFile)
  
  xNode = XMLNodeFromPath(RootXMLNode(xmlDoc), "plist/dict/key") 
  
  found.l = #False
  
  thisNode = xNode
  
  While found = #False
    
    If GetXMLNodeName(thisNode) = "key" And GetXMLNodeText(thisNode) = "Library Persistent ID"
      foundNode = NextXMLNode(thisNode)
      libID = GetXMLNodeText(foundNode)
      found = #True
    Else
      newNode = NextXMLNode(thisNode)
      If newNode = 0 : End : EndIf
      thisNode = 0
      thisNode = newNode
    EndIf
    
  Wend
  FreeXML(xmlDoc)
  ProcedureReturn libID
EndProcedure


OpenConsole()

url.s = "http://snapcorp.com"
loginpwd.s = "darrick_richardson@yahoo.com:password"



SetCurrentDirectory("D:\Projects\PlayLusting\")

libid.s = GetLibID("D:\Projects\PlayLusting\iTunes Music Library.xml")

fileToUpload.s = "tall_seattle.jpg"

boundary.s = "aA7hhfk4"

origBaseEnc = AllocateMemory(Len(loginpwd))
endBaseEnc = AllocateMemory(1024)
PokeS(origBaseEnc,loginpwd,Len(loginpwd))

howMuchEnc = Base64Encoder(origBaseEnc,Len(loginpwd),endBaseEnc,1024)

actionType.s = "POST /upload_file.php HTTP/1.0" + #CRLF$
host.s = "Host: www.snapcorp.com" + #CRLF$
connecttype.s = "Keep-Alive: 300" + #CRLF$
connecttype = connecttype + "Connection: keep-alive" + #CRLF$
agent.s = "User-Agent: PlayLusting Sync 1.0" + #CRLF$ + #CRLF$
auth.s = "";"Authorization: BASIC " + PeekS(endBaseEnc,-1,#PB_Ascii) + #CRLF$
majorContentType.s = "Content-type: multipart/form-data, boundary=" + boundary + #CRLF$
majorContentLength.s = "Content-Length: <length>" + #CRLF$
submit.s = "Content-Disposition: form-data; name=" + #DOUBLEQUOTE$ + "submit" + #DOUBLEQUOTE$ + #CRLF$ + "Submit"

boundary = "--" + boundary + #CRLF$

fileUploadField.s = boundary + "Content-Disposition: form-data; name=" + #DOUBLEQUOTE$ + "file" + #DOUBLEQUOTE$ +"; filename=" + #DOUBLEQUOTE$ + fileToUpload + #DOUBLEQUOTE$ + #CRLF$

minorContentType.s = "Content-Type: image/jpeg" + #CRLF$

;transferType.s = "Content-Transfer-Encoding: binary" + #CRLF$ + #CRLF$

fileLen = FileSize(fileToUpload)

*buff = AllocateMemory(fileLen)
ReadFile(0,fileToUpload);
ReadData(0, *buff, fileLen)

finalBoundary.s = ReplaceString(boundary,#CRLF$,"--",#PB_String_NoCase,3) 

requestHeader.s = actionType + host + connecttype + agent

finisher.s = #CRLF$ + boundary + submit + #CRLF$+ finalBoundary


postDataLength = Len(fileUploadField) + Len(minorContentType) + Len(#CRLF$)+fileLen + Len(finisher)

majorContentLength = ReplaceString(majorContentLength,"<length>",Str(5490))

inputData.s = requestHeader + majorContentType +  majorContentLength
inputData = inputData + fileUploadField + minorContentType + #CRLF$

OpenFile(1,"temppic.jpg")
OpenFile(2,"file.dat")


sendBuffer = AllocateMemory(Len(inputData) + fileLen + 512)

PokeS(sendBuffer,inputData)

CopyMemory(*buff,sendBuffer+Len(inputData),fileLen)

WriteData(2,sendBuffer,Len(inputData) + fileLen)
CloseFile(2)

PrintN(Str(FileSize("file.dat")))

PokeS(sendBuffer + Len(inputData) + fileLen,finisher)

If InitNetwork()
  conID = OpenNetworkConnection("www.snapcorp.com", 80) 
  If conID <> #Null
    res.l = SendNetworkData(conID,sendBuffer,postDataLength)
  Else
    PrintN("Error: Connection could not be made" )
    End
  EndIf
EndIf
getLen = 1000
mem = AllocateMemory(getLen)
quit = 0
WriteConsoleData(sendBuffer,Len(inputData) + fileLen+Len(finisher))
PrintN("")

Repeat

  NEvent = NetworkClientEvent(conID)
  If NEvent =2
    Start=1
    ;<- Resets timer every time Nevent=2 ->
    StartTime = ElapsedMilliseconds()
    Received_Lengt = ReceiveNetworkData(conID, mem, getLen)
    gotit.s =  PeekS(mem,Received_Lengt)
    If Received_Lengt = getLen : Quit = 2 : EndIf
  EndIf
  Delay(300)
  PrintN("Event: " + Str(NEvent))
Until Quit Or NEvent = 2
PrintN(gotit)

AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Well... What an ugly code! :shock:
Can only guess:
You wrote:majorContentLength.s = "Content-Length: <length>" + #CRLF$ "
Actually, you are not inserting the content length here, aren't you?

Oh,
Some lines further, I see that you're using ReplaceString()...

Well, I'm not willing to examine this code, maybe you can do me and others a favor and post the final HTTP-request?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
blackborg
User
User
Posts: 38
Joined: Thu Nov 02, 2006 8:20 pm

true.. I've been hacking on it for hours..

Post by blackborg »

POST /upload_file.php HTTP/1.0
Host: www.snapcorp.com
Keep-Alive: 300
Connection: keep-alive
User-Agent: PlayLusting Sync 1.0

Content-type: multipart/form-data, boundary=aA7hhfk4
Content-Length: 5490
--aA7hhfk4
Content-Disposition: form-data; name="file"; filename="tall_seattle.jpg"
Content-Type: image/jpeg

¶↕►↕↨§¶↑▲2!▲∟∟▲=,.$2I@LKG@FEPZsbPUmVEFdêemw{üéüN`ìùî}ûs~ü| █ C☺§↨↨▲→▲;!!;|SFS|||
||||||||||||||||||||||||||||||||||||||||||||||| └☺, █♥☺◄ ☻◄☺♥◄☺ ─ → ☻♥☺☺
☺☻♥♦♣♠ ─ 7► ☻☺☻♦♥♠♦♣♦☻♥ ☺☻♥◄♦↕!1AQa♣‼"2qæ¶§Rü3BSÆí4Cbr▒┴#$c ─ ↑☺☺☺☺☺☺
S°¬▼¡O≈"∞─╤⌂▐çεC`⌂◄G⌡a√É╪%▀R²H~ß░5Zƒ╫▼q░↔Σ>¿√ìü≈æ·ù╕╪â:·ù╕SS\╨♠d☺Öt ╠@≤¶,└;Çf ═╨
☺I ≤"♥2(yæ☺Ö ] ╙@;á♂áéΦ+─≈5~ù∞\♂║⌐┼2`§↓≥c♦╗Ö≥e└╒)«#≥♫εd└dÖpIBº1éJ£∩µc♥╔Y~oΣ`Æìo⌐
√î‼P─ZΩR≈↑↨■╩ⁿ╥≈↑‼₧%~y√î}ê_▄ƒ╣0☼‼ë[Uƒ╗↑☼ï┼/∩L`>7▬╢½!üⁿ├↓·Æ↑aⁿ╦↑┐;÷↑`∙«-~⌂αéK╡±V≤
/büv╞+£}ê$╗g‼■/∞♦ùm╫[╞,≤║▄a◄⌠5█ux╙ê☼τu?J>αv≥.K╪╓â$y/`▲H²+╪h;╕²+╪♥╗ç╙▼`♫Ωƒ╤▼`Ñ▄╙{
╙Å░╘Bt¿A^Pé√♂F
°╠-;┌ö_╪╟Θqéºhºu
4╫╪╗D~=┌▌═6∙╪ié↔áù₧ä▼ªä┌cíà»é─Φαú.ªºH▌≡8v╝ê║☼ùß▀÷╞ä√7♀╫ô∙→!≥£7╥²╞ü÷N↓≡~σ╤↨╪°⌂≥≈&
à≥j∟τε4/Æ╨·Ñε4☼▒h[═!ó?#Ñ┬o╪|4ƒa╙²G∞>♦√
▼¿²Ç>E←~'≡☻∙¶⌂W°▼☻∙☼  U∞>♫⌐♥E☺♥( ¬╜h╤åg÷D┤p±ÿ╔═╗┐▒ÅU╠¥G'v╦é)δp'♣vE9╟gm╨[◄î£]╨Go
│;I▌Q¼ε₧æô  ▓╩ò┌6å@ ¶♦ ♀♦♥ ░ §¶4♥ ☺Ç₧ëÉq1╕Ä≥ú⌂û;↑┐ZÄmd█■XVY-YQ,ûèΩAm↑∩Φ→ì8┌]▌
*VΣ←Ω|bπ~í╔8]♣z>╦┼≈⌠rM°ß╟Ü/5ÜΦ←@ ♥ 6 ☺Ç☺QC@ 0 (╞Tε≡≥k{Xöp*Ωæ╬4îíÜV4╥çO]╖a▬T
Ñ▓ΦE_é├9╩÷╤j←µ▼iJ.Qä]φ╜ü╫°┴*n+UkΩâ↑"⌠H#wgTt±q╖↨f'ú╥¥↑ 0►♀ Ç( d §¶0↑ ↑√O·uΩgÑ
Ä-Gk↓ì+î¢⌐÷(╖"║AZ⌐ß{╦_o∙2▄ëΓo↑*T▲D≈µ╩╢VEFPYú&⌠╠╦&│α₧→uuô⌠#Y¼⌡≡▌╠ò╡♀^p≡╖uπmε↓z╡▓6
┴ö 0 ↓☻░§ö 0 1÷Æo☼u┴ÖΘ\*·;⌠3→Q ΩQífp╜▌φ└5ì►╧NÆÜö║]è▄hm╘îf₧ªZ:t≤=è╕╜RI\b
│Γ⌐fâ♀uπ?ga▀─+¡.XσfGó6µ ` ♦ §ö 4♥ ☻¼E>≥îú╨öyΩ≡≡╔=╤═¿─╝╓A╢·◄╜ñƒ☻╡→ÑK4♥xXu'
yEΦc≥╘bxwJ∙û½ü→╤â¼òG }è╫7+½♂XÄ╥¬o%W¶φ↨╕f  å▓EµN┴q|gu╜└î▌╨f⌐èçz£ⁿ¿▒╧ºZéÄEôcúÆ╨
► ◄☺á ↑ ♣Ç,Çú◄äº^.Ω╥µK5\♀^◄╥¿┌╥q╒3§WaδτÑ~<Hδ(¬φ§%¼Öcq8föRQè╛φΩ╦½ƒ⌡u*Jܵ
KJö╢╗#5↑¡∟àK↔ZRSº▬╢hΘ<p╛ªT "♥@0 ↑ ♂Ç∟∞u,≈z#§¿Σ¿║Rr╛ⁿ♀╡+D$ñò°IRöεⁿ/f§d
½Z)-╪]V¬╣┼GÄ┴û╡♀░KáV£♀∩ A■Wºí╛kÅSδQª ♀♦ ☻ ☺ É♀♠ ♥ ▄
¬aíWIlEs√C♂
PäΓ╕┘Ö▒eaQn∙w2Θ¶╞Yg«üucôô≡ª▀@║╛ì'☼¶╖`n▐(¬«3tj)»║←î⌡5╥º8╘èö]╤╣u╞╠L¿ @ ►
α0 ↑♀ ♠ ♠|t;╠,╫§⌐Ü▒─º+L├º)╘é═{h←¡4;╝║E&§'←▓ïÜ▓☻⌐╞Ω─Bº₧↕═M┤─°═Ü┘♀WΩF▌V╞ºL^W╞QÆ╝
ñ¢n╚+-L}(;F≥}♀■ë↓+ΓΩ╓YWé=73ksûxσêjLJ1î«εé¥5ò½♣lî▲Φév|J♂ºfE┬ö│lèê┌r▌≈qO]X┴jº§←▓óÖ
╩◄▀ëò/♂WA*Q½R▲Y▀ú╘╗S≤*ΩX╡'ûó╩∙≡5:b≤ì&ÿ ♣0ÇüòA◄F#↨Né±;╦Æ%½#¢ê─╒─=φ▼Ñ↓nr«
╕ä►çà\b¼îR*♠ô(óñxó♀⌡+╩Qîb▌»½ «vU#«ΦÇòπ(╔z♣_▼▲¢2í║|╚,├╒qƒw'ß~W╚╘«}r╓m╠Ç d`♣↓1°«Γ

1≤╦°1n.kÖ↑╩ñ£Ñ½|L║H▓┤↔:.Kr┤₧▬î\<Z╢»p╘:xXτz╗zâ◄Tªª▌»¶┬cD6╣Dε♣╤~►"α¢╕↕OP¶µ`*Ä╘σ~@a
K2º§╠╚╜╤█■J╫⌠Vª▓╢
(╦X0═i£QQÆ¡Γε╕►┐IN◄ÆΓÄô╟▼Ω ► 0☻½ï┌↕r╞Jⁿ49_Zï0±V+íπ┐♣Gÿá┬┐
Sô╗ûù│µAb(Æm♦7-,@╘«PY░'ö♀╪╔<¬£wû■üJ◄MEÑ▒§rwH⌐Q⌐¼[ ═ò╔?T↓lα«QMUr♂░R╜∟┐K▒«\║⌡y
É0 áúàï╫↨S╘σ}j4╨≥ó║#ì╓£}E♠↨U÷◄cUè↑§┤σSó ¥¼∞♦╘o▒DjGæ♥àóÆe▬&₧─@è¬1q≡¬£┤~üT┬YZΣ╚.Z2
é[[Ö(╬πΓ√äkô╨"⌐╗óçâv½5╥σσ╧╢│L♠☻☻dS (α╓╫§?÷g/δq«ƒö¡íèⁿ+≥d☼♫£]║♠Ü╡4ä▌É♦#╗|H&ú¡°ü%
♂7-n└%▲-ò FⁿB£ag{Θ╚ X♣8τâïΓÇ├¶╒ß-╤♣╘█╦g║(£Æhè═^≡öy6D\ÑtT)¶,'⌡↕  Q↑Θ┤
█ÿ☺☺2)Ƕ♦∟→Übº■╠τ²n6S╪¡¬«∩V►ß╗☻Ωk┼r6╛≈╨╙(M^óKb♂6hèÆa↕E§╘╗n▄ !- ▒7─óD☺F|M+  ΣÄδ~ñ
◄è┌\↑‼
¬╡<⌠┌π╣♦(xíw├BóR╤§◄┴  S-⌂(ît▐m╠ ÇÖ¶♦☺L♫▲-d╞O╘τ}j4╙~↕║3╒ò±Zjóê▒ó5/↔4
£d⌡ké
¥▼-∙äX ÇÜ*▲]╨EO┴ ½#¿‼H♣9eï(╦O‼▐Tph╦WôO$▓=₧╠¼¢zàBÑEÆ╦p(ú<ò\^╥ VU╪¿º☼7N╝f÷z1↓│π
½╣╖" ☻DS
9↔ª¡ïOÜG>╜oÆäⁿ!╤M↓g£σ⌡2,_§╘4ôrì6ô╙ê*╚╘Pñ¢z╢òER7D*╘TJ↨Γé'mJ+½←╗Æ¿ªXï äúÖε♣*îa,╔jF
K═↔╩ç(«∞è╒â¬ΩQ│▐:3ñ«6cAY Hè e G←┤eƒ▬╫╥Æ9⌡δqT▐ZrΦ)ñ¡Φ→ì1αE,D▓╥ò╣\⌐U╤Æ»Aj)+U↑╕ó-hE
E░eC`BL)─üò♥`SRVDVi,╬∞♀Θd⌐$╕Ωé-L)8µ`e¡ÜìU8²╫2│ZcV5)½?╖"5↕∞·û─N∟$«kù.▌#nd♦╚á♠☻╪#â
Çîσd§Üwû╝ M♣e«╘*Eä⌐ikä4┬¬»←áî▓âO4↔ƒ@ï0⌡\q►ö╖╛¼Fz±▌╣╒╚☺2( ☻3väƒAQτe;7.º'NN◄╠≤=╪tì
┤╒òâI±µQN3≡%╘3╥£∟ôìÿº-q║z↓ió↕V☻w╕↨SVë¿êMÉN/Bä╪►{Ω☻ÆV☻ëk ¼ÿ┼t∞↕₧▲yΘ½∩─$J÷aEε¼└ªJ┴
n·é╗╓:3ñ«UyñZe¿═┌§2a¥₧»C6½çl╥Kü═╥4╙ìƒ@Φ╨₧üRN·üF1°
╧^3a]ª┼9tiú-º;▓¥⌠☻°╚¿ä⌡
ö▲ü♫KBèñEE;á!'`2V╒6X═Qà¥ñ╨f5I_`╥╗█p‼☻⌐½ä_┘Æ╦^Qα╒═rτ╙¿ta;ÖS╕ ▓ÄolN╤äy▄╟DsΘ∙î;
H╫↔âI¡☻ñÇ╧ï╓☼▄▒₧╝e├╗╔è╧♫ì)\╦¬φ╪↨E┘☺+Φ◄↓H¬pa▬9&é+₧íPIXé¬Ä┼↓+╦B─¼↔Ωú_╠ÜΦ╦ÄRσtaQJ+]
╨n]9F·ó*╖t♦$⌠☻■═ìδN_J╖╣«\╗t■╟F↕╣ò↨*§└π÷σX┬T█z█c↔Eè)5$ñ╢h├┤±ª♣ijDSûê¿┴ì»æ$¢M≥5↑ε▒
αk9Uq|ëc<_«╡ ]ÿw^┤`[▬♦╨Uu↔╡Ω◄(0,╓└FL")╪
¬▓î5█╩╦↓«<╓g⌐╖§╕\\¿5↓δ♫▓π»N¬½¶ΓεêΘ*yo╣§↨§m☻-∞Φ╡*»åƒ÷où>█εmâ╣☻rîU█Isa\╠wlBÆp├°º⌡=
æ4æ└¡VuªσRNMε┘↨→{>│wª⌡H═ì±⌂Ä¡5r:¡☻║⌡¶b▌⌠◄¢q├»U╘¢ô┘→s┐O☺ O◄Öl╖↔∟O«▌8[S¢╛/║░Sï`\₧Ç
BúyX►Ñ;┌┴↨⌐hTBRW /áUS╔Z¥╓àëcæV9*4═╟→q├╒¥9ISôîU█╢ê¿ì∟D≡╒§ƒçè♂.;Tj╞ñ‼╛ì↓╟I~%N♫╗j¢V
[╛E╞oX▌J£iAF&≤∟≈S╣Qτe┌╪Ö+*û⌠è1¡3U┼V½τ£ÑΩ╞àçú,Ux╥ï±KïαEt▼`╒[╫ºⁿùv]l-\εP£z↕¼∙[)╦Kl
♂^ 7←♣4é#$◄L╒╩*íNƒ─,±M╜‼kcQ╦╕█Vzqè╥)_æï^₧f5┼↓h≥ü8½q☻WΩ♣U½F1weD)M+F÷╨
ptƒûJ╟G7♣÷eW╞60½#┘°ê╞╩óKú♂ºO│▒¶σܧ¼·\#u(c#µ─'δ←ùQuδ~ó²úiÄM.═èⁿI7ΦY╩Z╛8,:~K·ù≥Ü╛î
)╤½↓┬◄ì╕ï>↕║◄╘╦fπ└ó⌐ßí-mg╨ÿ╗b▼♀÷╧ºí1»╥Jä↕┘}╤uï⌡ŧN↔σ≥½Ö╡yτΘ╥F▲ê╙↑ó)░→ëC╚║♦e┼Q╛»°
ÉåT¶╪♣ÇRój┼↓µ≥╔>Nσîu>6╒z&uqguß▬╓Wá─╥x¿/╩╞→_←Is'σt■.¢µ\MKΓ)s&.¿V▐µ╪> $╒ÖF·w▓⌠G↨EÑ
á î┤M╛ sf√┌ì£φtµ'♣òÖuhïe‼╦~♦↕H eOró5!∟¡/╕
▬┌%▬╪☻─♣Çä⌠EE-\
¬┼(äN↕╧çï{Ñc¼±╞·═êV¬·½¢ÄuÜúαèëB +╜└òôD◄╝F♂╒¼"ä∙¶→δ{☺╨Ä╤╖$q╛║&ÖC@;☺N.Y(╛║
èwa→c▬└n,á╦b"↕M½±\

jΘi¿▬á$ò╩ -♠
▬Ω¶'¿A╟p:►ⁿ:oⁿN7╫XÜ*ªé%`0vä∩8┴p╘╧U«QÑ←↑viñÇîúÆ}╦╨
ì¶₧à▬Z·ò
≈⌡ ,¼█
«►I╢╕ü7└"p╜è§GíE)fƒB ╘VD↓¬FΘüæ'→¡sF╣sφ^2:┬of¼uσ╞¬QV╨╙#ì╕◄EÆwzÇ│█d♣└B↕╗│
ò╨GJ▼â♫è╟→δ↕ASëEù▓m≡*92ô½ZR|╬U╫ÿ║*╚ì╡Q╤☺*ÉRÅSH«↕╦ú♥Ld¡╣ªU╘╤]◄J5▬F¼╔7╨á═Γ╛└Y↓>;¶F
r╗╨"PV@*ùhòY╓⌐ªAÜ╝mΓ\♦°ûj§╥₧▬/Θæ┌W₧│9-¼kY-~└7d╡╘♣º!ó╟ú░Tmgr☺½H♫¥7zqG*Φù►½"P▒♫╪y╡
╣ñ≈ S[¶XÆ▓♥ ┘╧╬╚*½░*êδçÜg^\:⌡û< ╢òÉ↕ƒå: û▒@
--aA7hhfk4
Content-Disposition: form-data; name="submit"
Submit
--aA7hhfk4--
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Re: true.. I've been hacking on it for hours..

Post by AND51 »

blackborg wrote:...User-Agent: PlayLusting Sync 1.0

Content-type: multipart/form-data, boundary=aA7hhfk4
...
Why do you have two #CRLF$ here? This is wrong, as the server thinks your request ends at this point. The rest is interpreted as content-upload.

This is the first thing that immediatle came to my mind. Don't know, if there are further errors in your code.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
blackborg
User
User
Posts: 38
Joined: Thu Nov 02, 2006 8:20 pm

probably other errors..

Post by blackborg »

I made the change you noted and this is the error response I got

Code: Select all

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>400 Bad Request</TITLE>
</HEAD><BODY>
<H1>Bad Request</H1>
Your browser sent a request that this server could not understand.<P>
Request header field is missing colon separator.<P>
<PRE>
--aA7hhfk4</PRE>
<P>
<HR>
<ADDRESS>Apache/1.3.33 Server at AppStuff Port 80</ADDRESS>
</BODY></HTML>
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Well if you get a code (400 in this case) it's quite good, as you know that your code works - but not the right way.

Well the message you got from the server is quite self explaining, isn't it?
What is unclear, if the server says, "Request header field is missing colon separator."

You must remove the line "--aA7hhfk4". What should this do in your request, can you tell me?
PB 4.30

Code: Select all

onErrorGoto(?Fred)
blackborg
User
User
Posts: 38
Joined: Thu Nov 02, 2006 8:20 pm

The original code generated this return code

Post by blackborg »

was

Code: Select all

Event: 2
HTTP/1.1 200 OK
Date: Sun, 28 Dec 2008 23:17:19 GMT
Server: Apache
Connection: close
Content-Type: text/html

Invalid file
"Invalid file" is echo'd by php.. so this part is working.. it's just that the file is not being uploaded and there is no data in $_POST or $_FILES?

After making the change you suggested with removing the double CRLF$ I started getting the "400 Bad Request"..

Hopefully this sheds more light..

I don't know if the content-length is incorrect.. if so, do you know how exactly to calculate it? or maybe something else is the problem..
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

As you said, the upload itself seems to be correct. The best thing would be to have a look at the PHP code. Obviously, it expects a very special file. In other words: Are you uploading the wrong file?

A "correct" upload should look like this: Header+CRLF+CRLF+Filecontent. You see? There must be only 1 double crlf.

The filesize can easiliy be retrieved with FileSize(), so there is no need for an extra routine to calculate the size.

Pitily, I don't have any PHP kowledge, I'm more familiar with CGI/Perl. So I don't know what $_POST and $_FILE is (I can only guess). Have you got access to the PHP code? I assume, it reads the uploaded file somehow and then decides, wether the file is valid or not. The fact, that it can decide that your file is invalid, leads me to the conclusion that the upload already works. However, you're uploading something else but not the expected file.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
blackborg
User
User
Posts: 38
Joined: Thu Nov 02, 2006 8:20 pm

Really Great.. thank you..

Post by blackborg »

I will keep inspecting it.. it works with a html form and live httpheaders.. I'm sure there's something simple and small I'm missing..

if there is other form data, the content-length refers to that file PLUS any other key/value/boundary info from the "form".. correct..

You've been really helpful..

Thanks again..
blackborg
User
User
Posts: 38
Joined: Thu Nov 02, 2006 8:20 pm

It Worked

Post by blackborg »

I checked on the server and it worked..

I had to use "Connection: closed" and pass the size of the file to Content-Length: <numbytes>..

worked like a charm.. many thanks..
blackborg
User
User
Posts: 38
Joined: Thu Nov 02, 2006 8:20 pm

Well Almost

Post by blackborg »

I got jpgs to upload to my php server..

but posting a tar.gz file to a ruby/rails/tomcat server.. does anyone have a working example of how to put multiple strings and binary data into a memory buffer in a specific order in PB..

also.. for http protocol.. can someone explain to me since I'm a noob at http coding.. how exactly is content-length calculated (what's the right number to put)..
deano1987
User
User
Posts: 19
Joined: Thu Jul 30, 2009 11:39 pm
Location: england, cannock, west midlands

source?

Post by deano1987 »

does anyone have the corrected source code?
deano1987
User
User
Posts: 19
Joined: Thu Jul 30, 2009 11:39 pm
Location: england, cannock, west midlands

my code is...

Post by deano1987 »

okay I have this code so far, it doesnt return error but the upload.php file does not upload anything??

my PB code

Code: Select all

Procedure.s DoHTTPPost()
  OpenConsole()

  url.s = "http://www.resplace.net/upload.php"
  
  
  SetCurrentDirectory("I:\Test\")
  
  fileToUpload.s = "untitled.png"
  
  boundary.s = "aA7hhfk4"
  
  actionType.s = "POST /upload.php HTTP/1.1" + #CRLF$
  host.s = "Host: www.resplace.net" + #CRLF$
  ;connecttype.s = "Keep-Alive: 300" + #CRLF$
  connecttype.s = "Connection: closed" + #CRLF$
  agent.s = "User-Agent: EmoteMaker Upload" + #CRLF$
  auth.s = "";"Authorization: BASIC " + PeekS(endBaseEnc,-1,#PB_Ascii) + #CRLF$
  majorContentType.s = "Content-type: multipart/form-data, boundary=" + boundary + #CRLF$
  fileLen = FileSize(fileToUpload)
  
  majorContentLength.s = "Content-Length: "+ Str(fileLen) + #CRLF$
  submit.s = "Content-Disposition: form-data; name=" + #DOUBLEQUOTE$ + "submit" + #DOUBLEQUOTE$ + #CRLF$ + "Submit"
  
  boundary = "--" + boundary + #CRLF$
  
  fileUploadField.s =  "Content-Disposition: form-data; name=" + #DOUBLEQUOTE$ + "file" + #DOUBLEQUOTE$ +"; filename=" + #DOUBLEQUOTE$ + fileToUpload + #DOUBLEQUOTE$ + #CRLF$
  
  minorContentType.s = "Content-Type: image/png" + #CRLF$
  
  ;transferType.s = "Content-Transfer-Encoding: binary" + #CRLF$ + #CRLF$
  
  fileLen = FileSize(fileToUpload)
  
  *buff = AllocateMemory(fileLen)
  ReadFile(0,fileToUpload);
  ReadData(0, *buff, fileLen)
  
  finalBoundary.s = ReplaceString(boundary,#CRLF$,"--",#PB_String_NoCase,3)
   
  requestHeader.s = actionType + host + connecttype + agent
  
  ;requestHeader.s = actionType + host + agent
  
  finisher.s = #CRLF$ + boundary + submit + #CRLF$+ finalBoundary
  
  
  postDataLength = Len(fileUploadField) + Len(minorContentType) + Len(#CRLF$)+fileLen + Len(finisher)
  
  
  inputData.s = requestHeader + majorContentType +  majorContentLength
  inputData = inputData + fileUploadField + minorContentType + #CRLF$
  
  OpenFile(2,"file.dat")
  
  
  sendBuffer = AllocateMemory(Len(inputData) + fileLen + 512)
  
  PokeS(sendBuffer,inputData)
  
  CopyMemory(*buff,sendBuffer+Len(inputData),fileLen)
  
  WriteData(2,sendBuffer,Len(inputData) + fileLen)
  CloseFile(2)
  
  PrintN(Str(FileSize("file.dat")))
  
  PokeS(sendBuffer + Len(inputData) + fileLen,finisher)
  
  If InitNetwork()
    conID = OpenNetworkConnection("www.resplace.net", 80)
    If conID <> #Null
      res.l = SendNetworkData(conID,sendBuffer,postDataLength)
    Else
      PrintN("Error: Connection could not be made" )
      End
    EndIf
  EndIf
  getLen = 1000
  mem = AllocateMemory(getLen)
  quit = 0
  WriteConsoleData(sendBuffer,Len(inputData) + fileLen+Len(finisher))
  PrintN("")
  
  Repeat
  
    NEvent = NetworkClientEvent(conID)
    If NEvent =2
      Start=1
      ;<- Resets timer every time Nevent=2 ->
      StartTime = ElapsedMilliseconds()
      Received_Lengt = ReceiveNetworkData(conID, mem, getLen)
      gotit.s =  PeekS(mem,Received_Lengt)
      If Received_Lengt = getLen : Quit = 2 : EndIf
    EndIf
    Delay(300)
    PrintN("Event: " + Str(NEvent))
  Until Quit Or NEvent = 2
  PrintN(gotit)
EndProcedure
my php code (should be correct (btw removed file from my server to stop hacker ;)))

Code: Select all


move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER["DOCUMENT_ROOT"]."/test.png");

//echo "The uploaded file size is " . $filesize . " bytes\n";
echo $_SERVER["DOCUMENT_ROOT"];

foreach($_POST as $key => $value)
{
	echo "Parameter name: " . $key . ", value: " . $value . "\n";
}

?>
please can anyone help me? thanks :)
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Nice example Deano!
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Rook: Huh? Nice example of something that appears not to work? :?
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Post Reply