Solved

internet server and cookies

Posted on 2000-03-17
16
277 Views
Last Modified: 2010-04-02
Hi
I'm writing an internet server
now the problem lets say that i have a server now a client connects to the server and requests a html document and the server send the html doc but the html need some cookie for it
how can i send a cookie with the html
<the client is a broweser that requests an html>

example:
the broweser request www.google.com
when i'm doing it without my server it get three files the html a gif pic and a cookie but when i'm doing it via my server i get the html and gif without the cookie the server gets the cookie but i don't know how to send it to the broweser
the broweser  requaest the html from the server the server gets the html from the net and get a cookie with it
i send the html to the broweser and close the connection <i tried to send the cookie with html i mean before and after the html text the resolte are the broweser add the cookie text to the html text and didn't make a cookie>
then the broweser opens the html and requests the gif and the server send the gif
no cookie
0
Comment
Question by:ntdragon
  • 8
  • 6
  • 2
16 Comments
 

Expert Comment

by:chuckle
ID: 2629645
Cookies are transmitted in the header of HTTP requests AND are available to the server on a domain basis. This simple statement will influence how you solve your problem.

You didn't give any detail on the tools\language you are using to develop your application or what platform you are developing on, so my answer will just be a general concept response.

Your architecture probably looks like this:
client <--> yourserver <--> realserver

Where information is passing back and forth between client and realserver through yourserver.

Let's assume that the request from the client to realserver looks like:
http://yourserver/cgi/getit.pl?url=realserver/index.html

This states that the client wants the index.html from realserver.

yourserver gets index.html from realserver and recieves a cookie in the header of the response. Now what yourserver really needs to do is pass this cookie down to the client, but the cookie itself has a "domain=" attribute that says realserver in it.

This means that when the client next requests a page the cookie doesn't makes it's way back from the client to yourserver, beacuse the cookie is marked as belonging to realserver.

What you need to do is intelligently modify the domain of the cookie so that the cookies are sent up from the client to yourserver, where you then modify the cookie back into it's original form and send the request off to realserver.

What are you using to make the requests from yourserver to realsever? You need something that either let's you build your own HTTP headers or a User Agent type thing that will let you use cookies.

For a complete description of the cookie specification check out:
http://home.netscape.com/newsref/std/cookie_spec.html

I can't stress how important the information in the above link is.

It sounds like you are trying to write a portal or a server application to get around a firewall. Is either of these quesses correct?

-ece
0
 

Expert Comment

by:krishnakumar
ID: 2630609
What technology you use in your server?
Servlets, CGI , asp ???
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2630744
sorry about that your answer intersting but it don't really help maybe you didn't understand the problem

so again
yes you are right
the coonection look like this
broweser<--->myserver<---->realserver

now my server is set to be a proxi for the broweser so i don't think i should modify the cookie
but this isn't the problem
the problem is
1)i'm writing it on C++Builder4
using the compenets TServerSocket and TNMHTTP
i have a Socket connection to the broweser and for geting the request from the net i use TNMHTTP<it get the cookie in one of it data members>
so the problem is when my server get the html and the cookie from the net <they are stored in to diffrent data members in the TNMHTTP class>
how can i send it back to the broweser via the Socket connection i installed
<in a way that the broweser will undertsand that he got two doc a html and a cookie>
0
 

Expert Comment

by:krishnakumar
ID: 2631953
Hi ntdragon,
I have not used C++builder4.
The general way you can pass the cookie to the browser is sending the cookie in the header.

The format for cookie looks like,
set-cookie: name=cookiename; value=cookievalue;expires=expirestime;domain=domain; path=path; secure;

let me know if this is what your looking for, i will explain the fields in detail.

KK
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2632068
you see the poblem is that i have a socket connection with the broweser
so the only thing i can do is to use the sendtext or sendbuf funcs to send the data to the broweser
what i'm doing is i'm using the sendtext
func to send the html text to the broswer
so tell me how should i send  the cookie text to the broweser<i know how does a cookie look>
using those funcs or maybe you know which another func i should look for
<name in another program or th idea of the func> and by the way i think that a socket is allmost the same everywhere

thanks
0
 

Expert Comment

by:chuckle
ID: 2632675
I haven't used C++ builder 4 either, but HTTP is almost the same everywhere.

If you are properly constructing your headers, the broswer should get it.

The broswer won't get 2 docs, it will get 1 doc with a cookie header preceeding it.

You are sending the data directly to the browser and not a Java app correct?

Post a minimal sample of what you are returning to the broswer.

If you are using a pure socket class (not one derived for a certain protocal) the stream should be clean.

You might want to use telnet to help with debugging. That way you can make sure of what exactly youserver is sending back to the client.


-ece
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2633463
i'm using a TServerSocket class
it allmost like the pure Socket class but you can take it as it's the same

now i'm sending the html text directlly

can you send me an examle of an html with cookie that i can send to check if it work and to see how it should look like?

second i don't exectlly know what are you refering to by header 'cause i got only the html text and the cookie text
what is the header?

i don't have any problem with debuging i know execttly what my prog to and when
the only problem is that as you said chuckel that i can send only one doc i all ready found that so how should i send in one doc the html and the cookie text in that way that the broweser will understand which is which and it will write the cookie to the hdd like it does if i'm not using my server <i mean when i'm using direct connection to the net>
0
 

Expert Comment

by:chuckle
ID: 2633605
I'm surprised that you don't know what a header is.

If you are using a socket class and not one that uses the HTTP protocal, you must be generating an HTTP header for the browser to understand your response.

I'll write an example of an HTTP response with a cookie in it, but in the meantime post the relevent section of your code that is generating the response back to the client.

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Expert Comment

by:chuckle
ID: 2633649
Check out the link:
http://www.wdvl.com/Internet/Protocols/HTTP/
for a good explanation of HTTP.

A sample HTTP response actually looks like this:
HTTP/1.0 200 OK
Date: Wednesday, 02-Feb-95 23:04:12 GMT
Server: NCSA/1.3
MIME-version: 1.0
Last-modified: Monday, 15-Nov-93 23:33:16 GMT
Content-type: text/html
Content-length: 59
Set-Cookie: foo=bar; path=/; expires=Mon, 01-Jan-2001 00:00:00 GMT
     * a blank line *
<HTML><HEAD></HEAD><BODY>This is what you see</BODY></HTML>

See the cookie?

If you are not writing something that looks like the above then I don't know how are sending anything back.

I wrote a bunch of C\CGI apps a while back and the header is important.

-ece
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2635023
now about my code it's very complex so i wrote a simple prog that do the connection like mine

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Ondata(TObject *Sender, TCustomWinSocket *Socket)
{
   String data;
   Label2->Caption ="Connect";
   Button1->Enabled =true;
   Socket1=Socket;
   data=Socket->ReceiveText();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   Socket1->SendText(Memo1->Text);
   Socket1->Close();
   Label2->Caption ="Disconnect";
   Button1->Enabled =false;
}
//---------------------------------------------------------------------------

now for chuckle i tried to send the example of the html that you gave in the last comment and i really got a cookie
but i get only:
foo=bar; path=/; expires=Mon, 01-Jan-2001 00:00:00 GMT
     
is it what should be in the cookie
what i mean is you wrote something before the set-cookie should it be in the cookie as well?
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2635820
Adjusted points from 200 to 240
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2635821
increasing to 240

thank to chuckle now i'm starting to understand it now tell me chuckle or anyone

abou the example:

HTTP/1.0 200 OK
Date: Wednesday, 02-Feb-95 23:04:12 GMT
Server: NCSA/1.3
MIME-version: 1.0
Last-modified: Monday, 15-Nov-93 23:33:16 GMT
Content-type: text/html
Content-length: 59
Set-Cookie: foo=bar; path=/; expires=Mon, 01-Jan-2001 00:00:00 GMT
     * a blank line *
<HTML><HEAD></HEAD><BODY>This is what you see</BODY></HTML>

i need an explaination of each line and tell me if have to put it or not if i do how to do it automaticlly

what i mean is the line:
Last-modified: Monday, 15-Nov-93 23:33:16 GMT
do i have to put this line if i do how to do it that way that it will find the data automatticly

i tried this example in my prog and it works the problem is when i try a site from the net<instead of this example>
i get only the :
"foo=bar; path=/; expires=Mon, 01-Jan-2001 00:00:00 GMT"
part and it doesn't work i mean the broweser doesn't create a cookie
so do i have to make the first part by my self??? or the HTTP component get it from the net two???<and i have to find how it does it>

0
 

Accepted Solution

by:
chuckle earned 240 total points
ID: 2637683
I'm glad your are starting to understand. It's a bit intimidating when you first start, but you'll get it eventually.

Your example code provides us with absolutely no information. What does "Label2->Caption ="Connect";" have to do with the discussion at hand?

"i need an explaination of each line ..." are you kidding? This is basic HTTP. I can write the app for you, but I'm expensive :).

If you intend to write a proxy-like application you need to at least have a basic knowledge of HTTP.

Go to:
http://www.wdvl.com/Internet/Protocols/HTTP/article.html
and read EVERYTHING on that page before you continue with your applicaiton. You are wasting your time if you do not.

After you have read the brief HTTP intro go and READ ALL for the cookie spec at:
http://home.netscape.com/newsref/std/cookie_spec.html
You are wasting your time if you do not.

After you have read BOTH of those pages come back here, and I'll try to help you out again.

Post some RELEVENT information about your application.

1. What is your client? Is it a browser?
2. How does it send requests to yourserver? Are you setting yourserver as the proxy using the browser settings? Is the request embedded in a link? Is the request attached to a form?
3. You are processing\modifying the response from the realserver? Do don't need to give proprietary details, but what are you doing?


-ece
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2639956
i tried to find a func that will give me the first part with the cookie <the header of the html> i think i found it now i"ll have to try to use it?
so i don't need an explaination how do get it.

about your questions:
1)my client is the broweser
2)the is a standrat broweser request,my server et to be a proxi to the broweser in the broweser setings.
3)i'm not modifying the response for now

about my example :

label1 is a "label" that you can put a text on it like "connect" or "disconnect"

memo1 is a multiline text box you can put there lines of text and modify it on run-time

socket,socket1 are pointers to sockets
standart sockets

the idea is that i'm waiting that the broweser will connect to my prog then when it does i send back to it the text that in the memo1 component
in the memo1 component i puted you example chuckle and it worked

i"ll check the sites you gave me later today and i"ll try to update my prog then i"ll come back here
0
 
LVL 1

Author Comment

by:ntdragon
ID: 2640318
thanks to all
chuckle you helped me alot now the cookie problem is solved
0
 

Expert Comment

by:chuckle
ID: 2645000
Glad to help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now