Shop OBEX P1 Docs P2 Docs Learn Events
Spinneret to remote server with credentials — Parallax Forums

Spinneret to remote server with credentials

daill1daill1 Posts: 8
edited 2013-02-02 12:38 in Propeller 1
Hey, I am trying to connect to a remote server via the spinneret by sending an HTTP Header POST request. The directory on the server I am trying to talk to is password protected, which I enabled through cPanel. I am wondering if anyone knows the correct syntax for sending the request in SPIN or if I need to know more information on the server side. My code so far:
PUB PostData(id) | st, size, idx, tempMask
  pst.str(string(13,"Post Data!",13))
  tempMask := tcpMask
  tcpMask := SetTcpSocketMaskById(id, 0)
  Socket.Close(id)
  pause(delay)
  InitializeSocket2(id)
  pause(delay)
  'Get My IP
  pst.str(string(13, "Getting Assigned IP ",13))
  Socket.Connect(id)
  pause(delay)
  repeat while !Socket.Connected(id)
  pst.str(string(13, "Sending Header ",13))                         
  
  StringSend(id, string("POST /spinneret/formtest.php  HTTP/1.1", 13, 10))
  StringSend(id, string("Keep-Alive: 115", 13, 10))
  StringSend(id, string("Host: rcc.cfbtechnologies.com", 13, 10))       
  StringSend(id, string("Connection: keep-alive", 13, 10))
  StringSend(id, string("Content-Type: application/x-www-form-urlencoded", 13, 10)) 
  StringSend(id, string("Content-length: 45", 13, 10, 13, 10))
  StringSend(id, string("Authorization: Basic cmNjOkFXVTc0XmJWVEJ2fg== ", 13, 10))            ' rcc:AWU74^bVTBv~   user:pass 
   pst.str(string(13, "Waiting for response",13,13))
    
    'Clear the buffer
    bytefill(@tempBuff, 0, TEMP_BUFFER)
    
    repeat until size := Socket.rxTCP(id, @rxdata)
    
    ' Find and save message body
    ' Write the message to the terminal
    idx := str.MatchPattern(@rxdata, string(13,10,13,10), 0, true)
    bytemove(@tempBuff, @rxdata+idx+4, strsize(@rxdata+idx+4)) 
    pst.str(@tempBuff)
    
    pst.str(string(13, "Disconnect and reset socket: "))
    pst.dec(id)
    pst.char(13)
  {repeat until size := Socket.rxTCP(id, @rxdata)
  pst.str(@rxdata)
  AppendLog(@rxdata)
  'Find and print the message body
  'idx := str.MatchPattern(@rxdata, string(13,10,13,10), 0, true)
  'pst.str(@rxdata+idx+4)
   }
  'Reset the socket
  Socket.Disconnect(id)
  InitializeSocket(id)
  
  'Reset the tcpMask
  tcpMask := tempMask
The response is a '401 Authorization Required' and I am assuming that the server wants it base64 encoded. I am beginning to wonder if this is even possible or if I need to get with support from my server provider. What I have found in my research is that I should be able to Post the username and password to the server in this way and that this is the acceptable way to access the cPanel password protected directory. Anyone have any ideas?

Comments

  • Mike GMike G Posts: 2,702
    edited 2013-01-31 11:35
    There's a Basic Authentication example in the Spinneret forum. The attached driver/libraries target the WizNet W5200, However, the authentication logic is not device specific. The Spinneret/W5100 driver/libraries counterpart can be found on Google code.
  • daill1daill1 Posts: 8
    edited 2013-01-31 13:26
    Thanks for replying, that seemed to help a little, knowing that it can be done. Is the Basic Authentication example saying that I need to send the WWW-Authenticate header as well as the Authorization: Basic Header? I am using the spinneret and W5100 and am pretty new to this, any help with the coding would be much appreciated. I entered as follows:
    StringSend(id, string("POST /spinneret/formtest.php  HTTP/1.1", 13, 10))
      StringSend(id, string("Keep-Alive: 115", 13, 10))
      StringSend(id, string("Host: rcc.cfbtechnologies.com", 13, 10))       
      StringSend(id, string("Connection: keep-alive", 13, 10))
      StringSend(id, string("Content-Type: application/x-www-form-urlencoded", 13, 10)) 
      StringSend(id, string("Content-length: 39", 13, 10, 13, 10))                      
      StringSend(id, string("WWW-Authenticate: Basic realm=", $22, "localhost", $22, 13, 10))
      StringSend(id, string("Content-length: 45", 13, 10, 13, 10))
      StringSend(id, string("Authorization: Basic cmNjOkFXVTc0XmJWVEJ2fg== ", 13, 10))
    
    I am still getting a 401 error.
  • Mike GMike G Posts: 2,702
    edited 2013-01-31 13:46
    Thanks for replying, that seemed to help a little, knowing that it can be done. Is the Basic Authentication example saying that I need to send the WWW-Authenticate header as well as the Authorization: Basic Header?
    The WWW-Authenticate: header causes the client's browser to display the username and password prompt. The username and password is base 64 encoded and persisted in every Authorization: header for the remainder of the session.

    Are you sure the username and password are correct? I just tried from a browser and received an error using the credentials provider in your post.
  • daill1daill1 Posts: 8
    edited 2013-01-31 15:10
    The user:pass combo was incorrect actually (thanks for pointing that out, I changed things without putting them back, oops). After fixing that I am still getting the 401 with a newly created user:pass of test:#bxFxX!ylSGp. Is the syntax of my code correct? I understand what these headers are supposed to do, but I'm not positive about how to implement them.
  • Mike GMike G Posts: 2,702
    edited 2013-02-01 11:00
    The username and password work but there's no page @ /spinneret/formtest.php?
  • daill1daill1 Posts: 8
    edited 2013-02-01 11:24
    Sorry Mike, I took it down while making some changes, it is back up again. Do I need to edit the strings I am trying to pass or are you able to gain access with what I have used?
  • Mike GMike G Posts: 2,702
    edited 2013-02-01 12:24
    I can authenticate with a browser. I'll try it with a Spinneret when I get home from work.
  • Mike GMike G Posts: 2,702
    edited 2013-02-02 07:48
    Yes, I can authenticate using the Spinneret. Here are the headers used...

    HTTP GET
    GET /spinneret/formtest.php HTTP/1.1
    Host: rcc.cfbtechnologies.com
    User-Agent: Wiz5100
    Authorization: Basic dGVzdDojYnhGeFgheWxTR3A=
    

    HTTP POST
    POST /spinneret/formtest.php HTTP/1.1
    Host: rcc.cfbtechnologies.com
    User-Agent: Wiz5100
    Content-Type: application/x-www-form-urlencoded
    Content-Length: 9
    Authorization: Basic dGVzdDojYnhGeFgheWxTR3A=
    
    name=Mike
    

    The Content-Length: header is required when submitting a POST. Otherwise your server will throw a 403 Forbidden followed by a 404 Not Found. The 404 is thrown because the designated error document does not exist. You should fix the later error...
  • daill1daill1 Posts: 8
    edited 2013-02-02 12:38
    That worked on my end too! I toyed with it so much yesterday, changing things around and putting it in different orders, etc. Thank you so much for saving me from that frustrating road block. I will soon be attempting to do FTP, which I have been looking into on the forums and am getting some good info. I'll start a new thread when I need some help with what I don't find. P.S. I realize I don't have the error doc's loaded yet, it's on the list. Many thanks again!
Sign In or Register to comment.