Thread View: news.software.nntp
16 messages
16 total messages
Started by Joshua
Sun, 30 Dec 2001 07:55
Forecasting the posting's size
Author: Joshua
Date: Sun, 30 Dec 2001 07:55
Date: Sun, 30 Dec 2001 07:55
23 lines
2009 bytes
2009 bytes
After the Server receiving POST command from the client,does he have any way to forecast the size of the article that the client will post? Usually,the server will dynamically requires memory for caching and processing the receiving posting.I can't find the grounds for the server to determine how much memory he has to apply for that? Yes,most server has his own upper limit of posting. But at extreme situation,a client can posting a very,very large article(containing attachment). The server has to receive the whole posting until get the ending sign string(CRLF.CRLF) before he get a chance to respond the client a piece of message such as "441 Error.Guy,too big article!". Of course,he can directly closes the conversation as soon as the upper limit reaches, but as if that he never get the chance to tell the client what's wrong with him. On this situation,the client can only receive the last respond from the server,usually is:"340 post your article,end with <CRLF>.<CRLF>" Any idea about this? Thanks for any advice.
Re: Forecasting the posting's size
Author: Jeffrey M. Vinocur
Date: Sun, 30 Dec 2001 10:04
Date: Sun, 30 Dec 2001 10:04
43 lines
2395 bytes
2395 bytes
In article <a0mi6f$qqr$1@mail.cn99.com>, Joshua <JoshuaIIII@hotmail.com> wrote: >After the Server receiving POST command from the client,does he have any way >to forecast the size of the article that the client will post? No >Usually,the server will dynamically requires memory for caching and >processing the receiving posting.I can't find the grounds for the server to >determine how much memory he has to apply for that? As you say, dynamically. If you find you need more, realloc() or equivalent. >Yes,most server has his own upper limit of posting. But at extreme >situation,a client can posting a very,very large article(containing >attachment). The server has to receive the whole posting until get the >ending sign string(CRLF.CRLF) before he get a chance to respond the client a >piece of message such as "441 Error.Guy,too big article!". True. >Of course,he can >directly closes the conversation as soon as the upper limit reaches, but as >if that he never get the chance to tell the client what's wrong with him. Other than wasting bandwidth and a small amount of processing time, it's not such a big deal to trash the data as it comes in (if that's what you decide to do) and reply when it's done. >Any idea about this? Thanks for any advice. INN just takes the article as it comes and allocates however much memory it needs. -- Jeffrey M. Vinocur jeff@litech.org
Re: Forecasting the posting's size
Author: Curt Welch
Date: Sun, 30 Dec 2001 21:17
Date: Sun, 30 Dec 2001 21:17
33 lines
2394 bytes
2394 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote: > After the Server receiving POST command from the client,does he have any > way to forecast the size of the article that the client will post? > > Usually,the server will dynamically requires memory for caching and > processing the receiving posting.I can't find the grounds for the server > to determine how much memory he has to apply for that? That's because you can't know. You simply have to allocate the memory as you read, or you can write the data to a file as you read. > Yes,most server has his own upper limit of posting. But at extreme > situation,a client can posting a very,very large article(containing > attachment). The server has to receive the whole posting until get the > ending sign string(CRLF.CRLF) before he get a chance to respond the > client a piece of message such as "441 Error.Guy,too big article!". Yes, that's exactly what you have to do. If it gets too large you still have to keep reading it from the NNTP stream, but you simply stop storing it. Nobody said writing a server would be easy. :) > Of > course,he can directly closes the conversation as soon as the upper limit > reaches, That would be wrong. -- Curt Welch http://CurtWelch.Com/ curt@kcwc.com Webmaster for http://NewsReader.Com/
Re: Forecasting the posting's size
Author: Joshua
Date: Sun, 30 Dec 2001 23:53
Date: Sun, 30 Dec 2001 23:53
81 lines
3901 bytes
3901 bytes
"Curt Welch" <curt@kcwc.com> wrote in message
news:20011230161737.549$XI@newsreader.com...
> Nobody said writing a server would be easy. :)
Dear Curt,I never say it's easy,I just have to do. I have it started. With
your help,the work gets on favorably. I know I still need your help. I don't
care any word,I just thanks for any reply.
Here comes the piece of code to handle the posting,anything I have lost?
//-------------------------------------------------------
int CBasicClient::receive_article(char *strBuf,int len)
{
char *pInChar;
int recved_len;
unsigned flag=0;
char strStopper[]="\x0d\x0a.\x0d\x0a";
do
{
for(pInChar=strBuf;pInChar<strBuf+len-1;pInChar++)
{
// Read input stream until last char of strStopper matches
recved_len=RecvRaw(pInChar,1,0);
if(0==recved_len)
{
pInChar-- ;
break ;
}
if( *pInChar == strStopper[5-1] && pInChar - strBuf + 1 >=5)
{
// Assume match until one of the stopper chars mismatches
int bMatch = TRUE ;
for( int pos = 5 - 1; pos > 0 && bMatch; pos-- )
if( *( pInChar - 5 + pos ) != strStopper[ pos - 1 ] )
bMatch = FALSE ;
if( bMatch )
break ;
}
}
if (pInChar<strBuf+len-1) //posting is not too large reachs limit,return
normal code
break;
memmove(strBuf,pInChar-5-1,5-1); //posting is over large,reset Buffer,read
the other stream,untill CRLF.CRLF,but return over-large sign back
pInChar=strBuf+5-1;
flag++;
}while(true);
if (0<flag)
return -1; //overflow
*(pInChar+1-5)='\0'; //cut off the ending CRLF.CRLF
return pInChar-strBuf+1-5;
}
///////////////////////////////////////////////////
1.strBuf is the Buffer memory for cacheing the inputing posting. I have
allocate a max size of memory equals the posting upper limit such as
512*1024,using the following code:
char *strBuf=new char[max_posting_size];
2.I If the inputing stream reachs the limit,pInChar will approach the end of
the strBuf. After exits from for{ ... ...}segment,I know if the posting is
too big. If this happens, I (1) keep the last 4 received bytes(maybe they
happen are "CRLF.CR"),(2)reset the point to the beginning of the
strBuf(regarded the kept 4 bytes),(3)go back to read the other data in the
input stream and save them in the same strBuf,once by once,till "CRLF.CRLF"
is got. If the posting is too large,the input data will be trash,upon that I
can use the same buffer again and again. Once it overflow,I dare overlay.
At the end of the function,I return a value for telling what's happened.
This works looks correct. But I find it's obviously slower than either INN
or Dnews. I don't know how they act here. Any advice about it will be very
appreciated.
Re: Forecasting the posting's size
Author: Curt Welch
Date: Mon, 31 Dec 2001 02:59
Date: Mon, 31 Dec 2001 02:59
159 lines
7348 bytes
7348 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote:
> "Curt Welch" <curt@kcwc.com> wrote in message
> news:20011230161737.549$XI@newsreader.com...
> > Nobody said writing a server would be easy. :)
>
> Dear Curt,I never say it's easy,I just have to do. I have it started.
> With your help,the work gets on favorably. I know I still need your help.
> I don't care any word,I just thanks for any reply.
>
> Here comes the piece of code to handle the posting,anything I have lost?
> //-------------------------------------------------------
> int CBasicClient::receive_article(char *strBuf,int len)
> {
> char *pInChar;
> int recved_len;
> unsigned flag=0;
> char strStopper[]="\x0d\x0a.\x0d\x0a";
>
> do
> {
> for(pInChar=strBuf;pInChar<strBuf+len-1;pInChar++)
> {
> // Read input stream until last char of strStopper matches
> recved_len=RecvRaw(pInChar,1,0);
Reading a byte at a time from a stream is normally too slow for good
performance. You will need to move the data in larger chunks to
make it fast (but fast of course is relative idea). byte-at-a-time might
be fast enough for you needs.
> if(0==recved_len)
> {
> pInChar-- ;
> break ;
> }
> if( *pInChar == strStopper[5-1] && pInChar - strBuf + 1 >=5)
> {
> // Assume match until one of the stopper chars mismatches
> int bMatch = TRUE ;
> for( int pos = 5 - 1; pos > 0 && bMatch; pos-- )
C is 0 based, not 1 based. Your code would be a lot clearer if
you learned to think 0 based instead of writting it as 1 based
and subtracting 1 everywhere.
> if( *( pInChar - 5 + pos ) != strStopper[ pos - 1 ] )
> bMatch = FALSE ;
> if( bMatch )
> break ;
> }
> }
> if (pInChar<strBuf+len-1) //posting is not too large reachs limit,
> return normal code
> break;
>
> memmove(strBuf,pInChar-5-1,5-1); //posting is over large,reset Buffer,
Bug 1. pInChar-5-1 is not the same as pInChar-(5-1). This will prevent
your code from seeing the ending CR LF . CR LF if it showed up right at the
end of your buffer.
> read the other stream,untill CRLF.CRLF,but return over-large sign back
> pInChar=strBuf+5-1;
Bug 2. pInChar gets reset when the for loop starts over so setting it
here is pointless and once again, your code would not correctly spot
the CR LF . CR LF if it happened right at the overflow point.
> flag++;
Sloppy but unimportant bug: If the input data overflows enough times, your
flag will wrap back to zero.
Just rename "flag" to "overflow" and set it to TRUE.
> }while(true);
> if (0<flag)
> return -1; //overflow
coding style issue: you null terminate the buffer below but you
exit above without null terminating it. That, along with other sloppy
coding styles could lead to a core-dump when the code that calls this
function does something stupid like a strlen() of the buffer even
when the return value is -1. If you null terminate the buffer normally,
then never exit without the buffer null terminated.
> *(pInChar+1-5)='\0'; //cut off the ending CRLF.CRLF
Semi-possible-bug: don't use null terminated buffers. POST data will
contain
nulls.
Or, you could use a null terminated buffer if you check for nulls and
reject the article if it contains a null.
> return pInChar-strBuf+1-5;
> }
Bug3: your code won't correctly see ". CR LF" as a null article.
Issue: You do know about dot escaping leading dots don't you? It might
be ok to ignore them like you have done here, assuming you did that because
you knew what you were doing and not because you didn't know about it...
>
> ///////////////////////////////////////////////////
>
> 1.strBuf is the Buffer memory for cacheing the inputing posting. I have
> allocate a max size of memory equals the posting upper limit such as
> 512*1024,using the following code:
512K is a very small limit. 1MB or 4MB are standard. And for post data
you should actually set the limit to a 1K or so below that to allow for
header expansion.
> char *strBuf=new char[max_posting_size];
>
> 2.I If the inputing stream reachs the limit,pInChar will approach the end
> of the strBuf. After exits from for{ ... ...}segment,I know if the
> posting is too big. If this happens, I (1) keep the last 4 received
> bytes(maybe they happen are "CRLF.CR"),(2)reset the point to the
> beginning of the strBuf(regarded the kept 4 bytes),(3)go back to read the
> other data in the input stream and save them in the same strBuf,once by
> once,till "CRLF.CRLF" is got. If the posting is too large,the input data
> will be trash,upon that I can use the same buffer again and again. Once
> it overflow,I dare overlay.
>
> At the end of the function,I return a value for telling what's happened.
You wrote all that in C, you didn't need to repeat it in english. :) The
approach is valid, but allocating a max-size buffer for every post is
a potential performace hog. Allocating a smaller size to start with and
expanding only when needed is a more typical approach (but once again
makes the code more complex).
> This works looks correct.
It's not. People with your level of coding skill don't normally try to
write stuff as complex as a news server. You tend to write code that
when tested lightly seems to work but when put to use in the real world
crashes on a regular basis. Typical PC programmer stuff. But I could
tell that from you first questions posted here even before you showed
us your code. Good luck with getting a stable server running.
> But I find it's obviously slower than either
> INN or Dnews. I don't know how they act here. Any advice about it will be
> very appreciated.
It's most likely the read-a-byte-at-a-time style of code that's making
it slow. You need to allocate a larger buffer, and then read as many
bytes as possible with one read (I don't know C++ so maybe it's also
the RecvRaw() function in general that's too slow also). Then you scan the
bytes you just read to check for a match with the CR LF DOT CR LF.
--
Curt Welch http://CurtWelch.Com/
curt@kcwc.com Webmaster for http://NewsReader.Com/
Re: Forecasting the posting's size
Author: Joshua
Date: Tue, 01 Jan 2002 15:39
Date: Tue, 01 Jan 2002 15:39
40 lines
2991 bytes
2991 bytes
"Curt Welch" <curt@kcwc.com> wrote in message news:20011230215952.153$nt@newsreader.com... > "Joshua" <JoshuaIIII@hotmail.com> wrote: > > "Curt Welch" <curt@kcwc.com> wrote in message > > news:20011230161737.549$XI@newsreader.com... > > // Read input stream until last char of strStopper matches > > recved_len=RecvRaw(pInChar,1,0); > > Reading a byte at a time from a stream is normally too slow for good > performance. You will need to move the data in larger chunks to > make it fast (but fast of course is relative idea). byte-at-a-time might > be fast enough for you needs. In fact I know I can read a block of data from the input stream at a time (as large as 16K),but I can't find a way to determine where to terminate the input data. You said that POST data will contain nulls,but I never find them. Must I search the *whole* input data string for <CRLF>.<CRLF> as the terminator? I think ...after all,it is a little bit unsuitable ... > Issue: You do know about dot escaping leading dots don't you? It might > be ok to ignore them like you have done here, assuming you did that because > you knew what you were doing and not because you didn't know about it... I only get and save what the client sends. I know dot escaping format,but I keep them either receiving from clients or send back to clients. > It's most likely the read-a-byte-at-a-time style of code that's making > it slow. You need to allocate a larger buffer, and then read as many > bytes as possible with one read (I don't know C++ so maybe it's also > the RecvRaw() function in general that's too slow also). Then you scan the > bytes you just read to check for a match with the CR LF DOT CR LF. RecvRaw() is a new function I re-write based on ::recv(). I will re-write my code according your advice. Thank you very much!
Re: Forecasting the posting's size
Author: Joshua
Date: Tue, 01 Jan 2002 16:52
Date: Tue, 01 Jan 2002 16:52
44 lines
2805 bytes
2805 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote in message
news:a0sldf$457$1@mail.cn99.com...
> "Curt Welch" <curt@kcwc.com> wrote in message
> news:20011230215952.153$nt@newsreader.com...
> > "Joshua" <JoshuaIIII@hotmail.com> wrote:
> > > "Curt Welch" <curt@kcwc.com> wrote in message
> > > news:20011230161737.549$XI@newsreader.com...
>
> > > // Read input stream until last char of strStopper matches
> > > recved_len=RecvRaw(pInChar,1,0);
> >
> > Reading a byte at a time from a stream is normally too slow for good
> > performance. You will need to move the data in larger chunks to
> > make it fast (but fast of course is relative idea). byte-at-a-time
might
> > be fast enough for you needs.
>
> In fact I know I can read a block of data from the input stream at a time
> (as large as 16K),but I can't find a way to determine where to terminate
the
> input data. You said that POST data will contain nulls,but I never find
> them. Must I search the *whole* input data string for <CRLF>.<CRLF> as the
> terminator? I think ...after all,it is a little bit unsuitable ...
I change my code into:
received_len=RecvRaw(strBuf+received_total,len-received_total,0);
if (0==received_len)
return 0; //Error
received_total+=received_len;
I have a question here.Is there any possibility that "0==received_len"
happens under heaven? I have test and found if the input stream is empty,
recv() just waits there for inputing. I guess maybe 0==received_len will
never occurs. If so, I think I can re-write the above code into:
received_total+=RecvRaw(strBuf+received_total,len-received_total,0);
Re: Forecasting the posting's size
Author: Joshua
Date: Tue, 01 Jan 2002 17:09
Date: Tue, 01 Jan 2002 17:09
21 lines
1946 bytes
1946 bytes
"Curt Welch" <curt@kcwc.com> wrote in message news:20011230215952.153$nt@newsreader.com... > > }while(true); > > if (0<flag) > > return -1; //overflow > > coding style issue: you null terminate the buffer below but you > exit above without null terminating it. That, along with other sloppy > coding styles could lead to a core-dump when the code that calls this > function does something stupid like a strlen() of the buffer even > when the return value is -1. If you null terminate the buffer normally, > then never exit without the buffer null terminated. Yes,you are right. But I must check the return value as soon as possible. If -1 is found,I am very clear what is the meaning,so I will never use strlen() or strBuf.
Re: Forecasting the posting's size
Author: Curt Welch
Date: Tue, 01 Jan 2002 20:42
Date: Tue, 01 Jan 2002 20:42
29 lines
2336 bytes
2336 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote: > said that POST data will contain nulls,but I never > find them. They are not normal, and normally should not be sent, but they do happen. You could should deal with the problem in an intelligent manner. Truncating the posting at the first null is not an intelligent manner. If you want to see one in real life, check out <3c321de3$0$75161$ba620e4c@news.skynet.be>. It ends with a string of NULLs. It also contains CRs as well which are not part of the CR LF EOL markers. > Must I search the *whole* input data string for <CRLF>.<CRLF> > as the terminator? I think ...after all,it is a little bit unsuitable ... You MUST check out the whole input data string. > I only get and save what the client sends. I know dot escaping format,but > I keep them either receiving from clients or send back to clients. That's a valid way to deal with the issue. Though it wouldn't hurt to check for a line beginning with a single DOT and reject the POST as invalid. -- Curt Welch http://CurtWelch.Com/ curt@kcwc.com Webmaster for http://NewsReader.Com/
Re: Forecasting the posting's size
Author: Curt Welch
Date: Tue, 01 Jan 2002 20:46
Date: Tue, 01 Jan 2002 20:46
14 lines
1785 bytes
1785 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote: > I have a question here.Is there any possibility that "0==received_len" I don't know what RecvRaw does. Like I said I don't know C++. But there's a chance it would happen if the connection was closed. TCP is not a "record" or transaction protocol, so there is no way to send a 0 length packet. So it will not happen under normal conditions. But it may happen under abnormal condidtions. -- Curt Welch http://CurtWelch.Com/ curt@kcwc.com Webmaster for http://NewsReader.Com/
Re: Forecasting the posting's size
Author: Curt Welch
Date: Tue, 01 Jan 2002 20:48
Date: Tue, 01 Jan 2002 20:48
11 lines
1584 bytes
1584 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote: > Yes,you are right. But I must check the return value as soon as possible. > If -1 is found,I am very clear what is the meaning,so I will never use > strlen() or strBuf. "clear meaning" has a way of getting lost over time. :) -- Curt Welch http://CurtWelch.Com/ curt@kcwc.com Webmaster for http://NewsReader.Com/
Re: Forecasting the posting's size
Author: Jeremy
Date: Wed, 02 Jan 2002 00:31
Date: Wed, 02 Jan 2002 00:31
19 lines
1417 bytes
1417 bytes
Joshua <JoshuaIIII@hotmail.com> wrote: > received_len=RecvRaw(strBuf+received_total,len-received_total,0); > if (0==received_len) > return 0; //Error > received_total+=received_len; > > I have a question here.Is there any possibility that "0==received_len" > happens under heaven? I have test and found if the input stream is empty, > recv() just waits there for inputing. I guess maybe 0==received_len will > never occurs. If RecvRaw() is returning the result of recv() then you should expect a zero; if I'm not mistaken, you'll get one if the socket was disconnected from the other end. -- Jeremy | jeremy@exit109.com
Re: Forecasting the posting's size
Author: Joshua
Date: Thu, 03 Jan 2002 04:29
Date: Thu, 03 Jan 2002 04:29
44 lines
2726 bytes
2726 bytes
"Curt Welch" <curt@kcwc.com> wrote in message news:20020101154241.937$wO@newsreader.com... > "Joshua" <JoshuaIIII@hotmail.com> wrote: > > said that POST data will contain nulls,but I never > > find them. > > They are not normal, and normally should not be sent, but they > do happen. You could should deal with the problem in an > intelligent manner. Truncating the posting at the first null > is not an intelligent manner. Did you mean that only "CRLF.CRLF" should be treated as the *exclusive* sign for ending the input stream? > If you want to see one in real life, check out > <3c321de3$0$75161$ba620e4c@news.skynet.be>. It ends with a string of NULLs. > It also contains CRs as well which are not part of the CR LF EOL markers. OK,I will check it. > > Must I search the *whole* input data string for <CRLF>.<CRLF> > > as the terminator? I think ...after all,it is a little bit unsuitable ... > > You MUST check out the whole input data string. > > > I only get and save what the client sends. I know dot escaping format,but > > I keep them either receiving from clients or send back to clients. > > That's a valid way to deal with the issue. Though it wouldn't hurt to > check for a line beginning with a single DOT and reject the POST as > invalid. > > -- > Curt Welch http://CurtWelch.Com/ > curt@kcwc.com Webmaster for http://NewsReader.Com/
Re: Forecasting the posting's size
Author: Joshua
Date: Thu, 03 Jan 2002 12:54
Date: Thu, 03 Jan 2002 12:54
11 lines
1698 bytes
1698 bytes
> If you want to see one in real life, check out > <3c321de3$0$75161$ba620e4c@news.skynet.be>. It ends with a string of NULLs. > It also contains CRs as well which are not part of the CR LF EOL markers. Would you mind to clone that article here? I have try to get it from it's local server but need authentication; I check the other news server but not find it. I search it by google,but no go.
Re: Forecasting the posting's size
Author: Curt Welch
Date: Thu, 03 Jan 2002 15:01
Date: Thu, 03 Jan 2002 15:01
10 lines
1564 bytes
1564 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote: > Did you mean that only "CRLF.CRLF" should be treated as the *exclusive* > sign for ending the input stream? Yes. -- Curt Welch http://CurtWelch.Com/ curt@kcwc.com Webmaster for http://NewsReader.Com/
Re: Forecasting the posting's size
Author: Curt Welch
Date: Thu, 03 Jan 2002 15:08
Date: Thu, 03 Jan 2002 15:08
22 lines
2309 bytes
2309 bytes
"Joshua" <JoshuaIIII@hotmail.com> wrote: > > If you want to see one in real life, check out > > <3c321de3$0$75161$ba620e4c@news.skynet.be>. It ends with a string of > NULLs. > > It also contains CRs as well which are not part of the CR LF EOL > > markers. > > Would you mind to clone that article here? I have try to get it from it's > local server but need authentication; I check the other news server but > not find it. I search it by google,but no go. It's just some porn spam that seems to have been posted with some type of broken bulk posting tool. Most articles that contain extra CRs or NULLs are spam or other types of "mistakes". I have no easy way to reproduce it here. The important issue is simply that your code needs to behave rationally if someone were to try and send an article in a POST command which contains NULLS and or extra CRs and/or extra LFs, etc. -- Curt Welch http://CurtWelch.Com/ curt@kcwc.com Webmaster for http://NewsReader.Com/
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads